C++(day8)

发布于:2025-09-09 ⋅ 阅读:(23) ⋅ 点赞:(0)

思维导图

list的相关函数实现

代码

#include <iostream>
#include <list>
using namespace std;

void printList(list<int> &l)
{
    list<int>::iterator iter;
    for(iter = l.begin();iter != l.end();iter++)
    {
        cout << *iter << " " ;
    }
    cout << endl;
}
int main()
{
    cout << "==========尾插===========" << endl;
    list<int> l;
    l.push_back(10);
    l.push_back(20);
    l.push_back(30);
    l.push_back(40);
    l.push_back(50);

    printList(l);

    cout << "==========赋值===========" << endl;
    list<int> l2(l);
    printList(l2);

    list<int> l3(l2.begin(),l2.end());
    printList(l3);

    list<int> l4(6,6);
    printList(l4);

    l=l4;
    printList(l);

    cout << "==========交换===========" << endl;
    list<int> l5(l2);
    printList(l5);
    printList(l);
    cout << "交换后:" << endl;
    l5.swap(l);
    printList(l5);
    printList(l);



    cout << "=========大小操作=========" << endl;
    if(!l.empty())
    {
        cout << l.size() << endl;
    }

    l.resize(10);
    printList(l);

    l.resize(3);
    printList(l);

    l.resize(7,7);
    printList(l);

    cout << "===========尾删===========" << endl;
    l5.pop_back();
    printList(l);

    cout << "===========头插===========" << endl;
    l5.push_front(9);
    printList(l);

    cout << "===========头删===========" << endl;
    l.pop_front();
    printList(l);

    cout << "===========指定位置插入===========" << endl;
    list<int>::iterator it = l.begin();
    it++;
    l.insert(it, 90);
    cout << "在第二位插入90后: ";
    printList(l);
    it++;
    l.insert(it,2,88);
    cout << "在第三位插入两个88后: ";
    printList(l);

    cout << "===========指定位置删除===========" << endl;
    it++;
    cout << "在第四位删除一位后: ";
    l.erase(it);
    printList(l);

    cout << "===========数据存取===========" << endl;
    int a=l.front();
    cout << "第一位为:" << a << endl;
    int b=l.back();
    cout << "最后一位为:" << b << endl;


    cout << "===========清除所有值为7的元素===========" << endl;
    l.remove(7);
    printList(l);


    cout << "===========清除所有元素===========" << endl;
    l.clear();
    printList(l);




    return 0;
}

运行结果:

封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个)

再把该容器中的对象,保存到文件中。

再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。

代码

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <stdexcept>

using namespace std;

class Student
{
private:
    string name;
    int age;
    string major;

public:
    Student() : name(""), age(0), major("")
    {}
    Student(string n, int a, string m) : name(n), age(a), major(m)
    {}

    string getInfo() const
    {
        return "姓名: " + name + ", 年龄: " + to_string(age) + ", 专业: " + major;
    }

    friend ostream& operator<<(ostream& os, const Student& s);
    friend istream& operator>>(istream& is, Student& s);
};


ostream& operator<<(ostream& os, const Student& s)
{
    os << s.name << "\n";
    os << s.age << "\n";
    os << s.major << "\n";
    return os;
}


istream& operator>>(istream& is, Student& s)
{
    if (!getline(is, s.name))
    {
        return is;
    }

    string ageStr;
    if (!getline(is, ageStr))
    {
        return is;
    }

    try
    {
        s.age = stoi(ageStr);
    } catch (const invalid_argument& e)
    {
        return is;
    } catch (const out_of_range& e)
    {
        return is;
    }

    if (!getline(is, s.major))
    {
        return is;
    }

    return is;
}

int main()
{
    vector<Student> students;
    students.push_back(Student("张三", 20, "Computer Science"));
    students.push_back(Student("李四", 21, "Mathematics"));
    students.push_back(Student("王五", 19, "Physics"));

    ofstream outFile("students.txt");
    if (!outFile.is_open())
    {
        cerr << "无法打开文件" << endl;
        return 1;
    }

    for (const auto& student : students)
    {
        outFile << student;
    }
    outFile.close();
    cout << "学生数据已保存到文件。" << endl;

    vector<Student> loadedStudents;
    ifstream inFile("students.txt");
    if (!inFile.is_open())
    {
        cerr << "无法打开文件" << endl;
        return 1;
    }

    Student temp;
    while (inFile >> temp)
    {
        loadedStudents.push_back(temp);
    }

    if (!inFile.eof())
    {
        cerr << "文件读取失败" << endl;
    }

    inFile.close();
    cout << "学生数据已从文件读取。" << endl;

    cout << "\n从文件中读取的学生信息:" << endl;
    for (const auto& student : loadedStudents)
    {
        cout << student.getInfo() << endl;
    }

    return 0;
}

运行结果


网站公告

今日签到

点亮在社区的每一天
去签到