例程
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
using namespace std;
class student{
public:
student(const string &a, int b):name(a), score(b){}
string name;
int score;
bool operator < (const student &m)const {
return score< m.score;
}
};
bool pass(student x)
{
if( x.score >= 60 )
return true;
else
return false;
}
int main() {
vector< student> vect;
student st1("Tom", 74);
vect.push_back(st1);
st1.name="Jimy";
st1.score=560;
vect.push_back(st1);
st1.name="Mary";
st1.score=92;
vect.push_back(st1);
st1.name="Jessy";
st1.score=85;
vect.push_back(st1);
st1.name="Jone";
st1.score=560;
vect.push_back(st1);
st1.name="Bush";
st1.score=522;
vect.push_back(st1);
st1.name="Winter";
st1.score=77;
vect.push_back(st1);
st1.name="Andyer";
st1.score=63;
vect.push_back(st1);
st1.name="Lily";
st1.score=76;
vect.push_back(st1);
st1.name="Maryia";
st1.score=89;
vect.push_back(st1);
student * re;
cout<<"------before sort..."<<endl;
for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
//pass 内返回为true的排序后在前面。
//re 返回的是两部分的交界处值为false的元素,但有可能所有的值都返回true,所以在使用re之前要先进行判断
re=stable_partition( vect.begin(), vect.end(), pass);
if(re<vect.end()) //要注意判断
cout<<"/n/n/n"<<re->name<<" "<<re->score <<"/n/n";
cout <<"-----after sort ...."<<endl;
for(i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
return 0 ;
}