【STL系列】结构体排序

#include<iostream>
#include
<algorithm>
#include
<vector>
#include
<string>
#include
<functional>
using namespace std;
class Student
{
int m_iChinese;
int m_iMath;
std::
string m_strName;
public:
Student(
const string& strName,const int iChinese,int iMath)
:m_iChinese(iChinese),m_iMath(iMath),m_strName(strName)
{}
bool operator <(const Student &student) const
{
if(m_iMath < student.m_iMath)
return false;
else if(m_iChinese < student.m_iChinese)
return false;
return true;
}
void print()
{
cout
<<m_strName<<":"<<m_iMath<<":"<<m_iChinese<<endl;
}
};

int main()
{
vector
<Student> student;
student.push_back(Student(
"liuda",96,100));
student.push_back(Student(
"liuhao",98,99));

sort(student.begin(),student.end());
for_each(student.begin(),student.end(),mem_fun_ref(
&Student::print));
}

posted on 2011-05-19 14:53  51Liuda  阅读(247)  评论(0编辑  收藏  举报

导航