10_13C++

#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;
}

你可能感兴趣的:(c++,开发语言)