文章来源:https://www.toymoban.com/news/detail-684339.html
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Stu
{
friend ofstream &operator<<(ofstream &ofs, Stu &v);
friend int operator>>(ifstream &ifs, Stu &t);
friend ostream &operator<<(ostream &cout, const Stu &O);
string name;
int age;
public:
Stu() {}
Stu(string name, int age):name(name), age(age){}
};
ofstream &operator<<(ofstream &ofs, Stu &v){
ofs << v.name << endl << v.age << endl;
return ofs;
}
int operator>>(ifstream &ifs, Stu &t){
if(!(ifs >> t.name))
return 0;
ifs >> t.age;
return 1;
}
ostream &operator<<(ostream &cout, const Stu &O){
cout << O.name << endl;
cout << O.age << endl;
return cout;
}
int main()
{
vector<Stu> v;
v.push_back(Stu("zhangsan",18));
v.push_back(Stu("lisi",18));
v.push_back(Stu("wangwu",18));
ofstream ofs;
ofs.open("E:/Cdaima/1.txt", ios::out);
vector<Stu>::iterator iter = v.begin();
while(iter != v.end()){
ofs << *iter++;
}
ofs.close();
ifstream ifs;
ifs.open("E:/Cdaima/1.txt", ios::in);
vector<Stu> v2;
Stu t;
while(ifs >> t){
v2.push_back(t);
}
iter = v2.begin();
while(iter != v2.end()){
cout << *iter++ << endl;
}
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-684339.html
到了这里,关于8.29 auto关键字 lambda 类型转换 标准模板库 文件操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!