Vector之类的遍历

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

}

推荐阅读更多精彩内容