美文网首页
Vector之类的遍历

Vector之类的遍历

作者: 人不知QAQ | 来源:发表于2019-12-30 17:07 被阅读0次

    #include <iostream>

    using namespace std;

    #include <algorithm>

    #include <vector>

    #include<set>

    #include <string>

    class Cstudent

    {

    public:

    Cstudent(int Id, string Name)

    {

    S_Id = Id;

    S_Name = Name;

    }

    public:

    int S_Id;

    string S_Name;

    };

    bool Compare(const Cstudent &stuA, const Cstudent &stuB)

    {

    return (stuA.S_Id < stuB.S_Id);

    }

    int main()

    {

    // vector<int> vecInt;

    // vecInt.push_back(1);

    // vecInt.push_back(7);

    // vecInt.push_back(3);

    // vecInt.push_back(7);

    // vecInt.push_back(9);

    //

    //// sort(vecInt.begin(), vecInt.end());//默认从小到大

    vector <Cstudent> vecStu;

    vecStu.push_back(Cstudent(2, "老二"));

    vecStu.push_back(Cstudent(1, "老大"));

    vecStu.push_back(Cstudent(3, "老三"));

    vecStu.push_back(Cstudent(4, "老四"));

    sort(vecStu.begin(), vecStu.end(), Compare);

    for (vector<Cstudent>::iterator it = vecStu.begin(); it != vecStu.end(); it++)

    {

    cout << it->S_Id << ":" << it->S_Name << endl;

    }

    system("pause ");

    return 0;

    }

    相关文章

      网友评论

          本文标题:Vector之类的遍历

          本文链接:https://www.haomeiwen.com/subject/oymmoctx.html