- #include
- #include
- #include
//包含头文件 -
- using namespace std;
- class Stu
- {
- public:
- string name;
- int age;
- public:
- Stu(){};
- Stu(string n,int a):name(n),age(a){};//有参构造函数
-
- };
- //容器的大小empty()
- //写入数据到文件中
- void printfVector(vector
&v) //算法(自己封装的算法&引入外部对象) - {
- //创建流对象
- ofstream ofs;
- ofs.open("D:/day7_code/tex1.tex",ios::out);
- //打开文件
- vector
::iterator iter;//迭代器 - for(iter = v.begin();iter< v.end();iter++)
- {
- //写入文件
- ofs << iter->name << " " << iter->age << endl;
- }
- cout << endl;
-
- }
- void readVector(vector
& v) - {
- ifstream ifs;
- ifs.open("D:\\day7_code\\tex1.tex", ios::in);
-
- vector
::iterator iter; - for (iter = v.begin(); iter != v.end(); iter++)
- {
- // string line;
- // getline(ifs, line);
- // cout << line << endl;
- char buff[1024];
- while(ifs >> buff)
- {
- cout << buff << endl;//每次读到空格或者换行截止
- }
-
- }
-
- cout << endl;
- }
- int main()
- {
- //创建一个容器
-
- Stu s("zhangsan",34);
- Stu s1("lisi",18);
- Stu s2("wangwu",33);
-
- vector
v; - v.push_back(s);
- v.push_back(s1);
- v.push_back(s2);//将数据存到容器中
- printfVector(v);
- readVector(v);
-
-
-
- return 0;
- }