C++ Day7

课后作业:

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> A;
    list<int>::iterator lit;
    A.assign(5,15);
    lit=A.begin();
    cout << "A:";
    for(int i=0;i<5;i++,++lit)
    {
        cout << *lit << " ";
    }
    cout << endl << "size: " << A.size() << endl;
    A.push_front(11);
    A.push_back(12);
    A.push_back(13);
    A.push_back(14);
    A.push_back(15);
    lit=A.begin();
    A.resize(10);
    for(int i=0;i<10;i++,++lit)
    {
        cout << *lit << " ";
    }
    cout << endl << "front:" << A.front() << endl;
    cout << "back:" << A.back() << endl;
    int a=15;
    A.remove(a);
    cout << "删除"<< a <<"后为:";
    for(int a:A)
    {
        cout << a << " ";
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

class Stu
{
    friend istream& operator>>(istream& is, Stu& student);
    friend ostream& operator<<(ostream& os, const Stu& student);
private:
    string name;
    int age;
public:
    Stu(){}
    Stu(string name,int age):name(name),age(age){}
    string getname()
    {
        return name;
    }
    int getage()
    {
        return age;
    }
};

ostream& operator<<(ostream& os, const Stu& student)
{
    os << student.name << " " << student.age;
    return os;
}
istream& operator>>(istream& is, Stu& student)
{
    is >> student.name >> student.age;
    return is;
}

int main()
{
    Stu a("czh",23);
    vector<Stu> v1(3,a);
    ofstream ofs;
    ofs.open("E:/1.txt",ios::out);

    for (Stu &a : v1)
    {
        ofs << a.getname() << " " << a.getage()<< endl;
    }
    ofs.close();

    ifstream ifs;
    ifs.open("E:/1.txt",ios::in);
    vector<Stu> v2;
    Stu temp;
    while (ifs >> temp)
    {
        v2.push_back(temp);
    }

    ifs.close();
    for (Stu &student:v2)
    {
    cout << "Name: " << student.getname() << ", Age: " << student.getage() << endl;
    }
    return 0;
}

思维导图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值