课后作业:
#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;
}
思维导图: