C++ DAY 7

list:

#include 
#include 
using namespace std;
void pi(list &i)
{
    list::iterator iter;
    for(iter = i.begin(); iter != i.end() ; iter++ )
    {
        cout<< *iter <<" ";
    }
    cout< i;
    i.push_back(10);
    i.push_back(20);
    i.push_back(30);
    i.pop_back();
    pi(i);
    list i1(i.begin(),i.end());
    pi(i1);
    list i2(6,6);
    pi(i2);
    list i3=i2;
    pi(i3);
    //赋值和交换
    list i4;
    i4.assign(i.begin(),i.end());
    pi(i4);
    i4.assign(5,5);
    pi(i4);
    list i5;
    i5=i4;
    pi(i);
    pi(i5);
    i5.swap(i);
    pi(i);
    pi(i5);
    cout<::iterator iter=i5.begin();
    advance(iter,2);//是迭代器的指向改变
    i5.insert(iter,11);
    pi(i5);
    i5.insert(iter,2,55);
    pi(i5);
    i5.erase(iter);
    pi(i5);
    i5.remove(55);
    pi(i5);
    i5.clear();

    return 0;
}

 

试编程:

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

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

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

#include 
#include 
#include 
using namespace std;
class Stu
{
private:
    string name;
    int age;
   friend ostream &operator<<(ostream &cout,const Stu &p);
  friend istream &operator>>(istream &ifs,Stu &p);

public:
    Stu(){}
    Stu(string name,int age):name(name),age(age)
    {}

};
istream &operator>>(istream & ifs,Stu &p)
{
    ifs>>p.name;
    ifs>>p.age;
    return ifs;
}
ostream &operator<<(ostream &ofs,const Stu &p)
{
    ofs< v;
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    ofstream ofs;
    ofs.open("E:/c/b.txt",ios::out);

    ofs<< v.at(0)< s;
    ifs>>s1;
    ifs>>s2;
    ifs>>s3;
    s.push_back(s1);
    s.push_back(s2);
    s.push_back(s3);
    for(int i=0;i<3;i++)
    {
        cout<

你可能感兴趣的:(c++)