#include
#include
#include
using namespace std;
class Stu
{
friend Stu operator<<(ofstream &o,Stu &k);
friend Stu operator>>(ifstream &i,Stu &k);
friend void printfvector( vector<Stu> &v);
private:
string name;
int age;
double score;
public:
Stu() {}
Stu(string n,int a,double s):name(n),age(a),score(s)
{}
void show()
{
cout << name << age << score << endl;
}
};
Stu operator<<(ofstream &o,Stu &k)
{
o << k.name << " " << k.age << " " << k.score << endl;
return k;
}
Stu operator>>(ifstream &i,Stu &s)
{
i >> s.name >> s.age >> s.score;
return s;
}
void printfvector( vector<Stu> &v)
{
vector<Stu>::iterator iter;
for( iter = v.begin(); iter != v.end();iter++)
{
cout << iter->name <<" " << iter->age << " " << iter->score << endl;
}
}
int main()
{
ofstream ofs;
ofs.open("D:/qt-file/1.txt",ios::out);
vector<Stu> kun1;
Stu s1("坤坤",1,2.5);
Stu s2("小黑子",11,5.0);
Stu s3("速删",111,7.5);
kun1.push_back(s1);
kun1.push_back(s2);
kun1.push_back(s3);
vector<Stu>::iterator iter;
for(iter=kun1.begin();iter !=kun1.end();iter++)
{
ofs<< *iter;
}
ofs.close();
vector<Stu> kun2;
ifstream ifs;
ifs.open("D:/qt-file/1.txt",ios::in);
Stu s4,s5,s6;
ifs >> s4;
ifs >> s5;
ifs >> s6;
kun2.push_back(s4);
kun2.push_back(s5);
kun2.push_back(s6);
ifs.close();
printfvector(kun2);
return 0;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75