把list的相关函数都实现出来
#include <iostream>
#include <list>
using namespace std;
//算法
void printlist(list<int> &v)
{
list<int>::iterator iter; //定义了这样容器类型的迭代器
for(iter = v.begin(); iter != v.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
}
int main()
{
//容器
list<int> v; //无参构造函数
cout << "-------尾插--------" << endl;
v.push_back(10); //尾插
v.push_back(11);
v.push_back(12);
v.push_back(13);
v.push_back(14);
v.push_back(15);
printlist(v);
cout << "-------头插--------" << endl;
v.push_front(110);//头插
printlist(v);
cout << "-------尾删--------" << endl;
v.pop_back();//尾删
printlist(v);
cout << "-------头删--------" << endl;
v.pop_front();//头删
printlist(v);
cout << "-------Over 插入--------" << endl;
v.insert(v.begin(),20);
//算法
printlist(v);
return 0;
}
--------------------------------
-------尾插--------
10 11 12 13 14 15
-------头插--------
110 10 11 12 13 14 15
-------尾删--------
110 10 11 12 13 14
-------头删--------
10 11 12 13 14
-------Over 插入--------
20 10 11 12 13 14
封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个)
再把该容器中的对象,保存到文件中。
再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Stu
{
//public:
friend void printVectora(vector <Stu> &studentA);
friend void printVectorb(vector <Stu> &studentB);
friend void printVectorc(vector <Stu> &studentB);
string name;
int id;
int age;
public:
Stu(){}
Stu(string na,int id,int age):name(na),id(id),age(age){}
};
void printVectora(vector <Stu> &studentA);
void printVectora(vector <Stu> &studentA)
{
ofstream ofp;
ofp.open("stu.txt",ios::out);
if(!ofp)
{
cout << "open->" << endl;
}
vector <Stu> :: iterator k1;
for(k1 = studentA.begin();k1 != studentA.end(); k1++)//v.endl 表示最后个元素的下一个元素
{
ofp << k1->name << " " ;
ofp << k1->id << " ";;
ofp << k1->age << endl;
}
ofp.close();
return ;
}
void printVectorb(vector <Stu> &studentB);
void printVectorb(vector <Stu> &studentB)
{
ifstream ifp;
ifp.open("stu.txt",ios::in);
if(!ifp)
{
cout << "open->" << endl;
}
vector <Stu> :: iterator k2;
int m1 = 0;
string s1 = "";
for(k2 = studentB.begin();k2 != studentB.end();)//v.endl 表示最后个元素的下一个元素
{
while(ifp >> s1)//读取name
{
k2->name = s1;//写入name
//cout << k2->name << " ";
ifp >> m1;//读取id
k2->id = m1;//写入id
//cout << k2->id << " ";
ifp >> m1;//读取age
k2->age = m1;//写入age
//cout << k2->age << endl;
k2++;
}
}
ifp.close();
return ;
}
void printVectorc(vector <Stu> &studentB);
void printVectorc(vector <Stu> &studentB)
{
vector <Stu> :: iterator k3;
for(k3 = studentB.begin();k3 != studentB.end(); k3++)//v.endl 表示最后个元素的下一个元素
{
cout << k3->name << " ";
cout << k3->id << " ";
cout << k3->age << endl;
}
return ;
}
int main()
{
Stu stu1("哈利波特",11,18);
Stu stu2("基尔霍夫",12,19);
Stu stu3("哈姆雷特",13,20);
Stu stub1(" ",0,0);
Stu stub2(" ",0,0);
Stu stub3(" ",0,0);
vector <Stu> studentA;
vector <Stu> studentB;
studentA.push_back(stu1);
studentA.push_back(stu2);
studentA.push_back(stu3);
studentB.push_back(stub1);
studentB.push_back(stub2);
studentB.push_back(stub3);
printVectora(studentA);
printVectorb(studentB);
printVectorc(studentB);
cout << "执行完毕" << endl;
return 0;
}
---------------------------------
哈利波特 11 18
基尔霍夫 12 19
哈姆雷特 13 20
执行完毕
完成思维导图。