学,学,死了也要学!没有理由不学!没有理由不爱C++!
#pragma warning (disable : 4786) //消除vc++6.0中vector<string>产生的警告
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm> //用到标准库提供的排序算法
using namespace std;
int convert(const string &s){ //转换string为int
istringstream in(s);
int ival;
in >> ival;
return ival;
}
string::const_iterator getNbrPosn(const string &s){ //获取string中首个数字位迭代器
string::const_iterator p = s.begin();
for (;p != s.end();++p)
if (*p == '=')
return p+1;
return p;
}
bool moreThan(const string &s1,const string &s2){ //谓词函数,按关键字(int)比较大小
string sub1(getNbrPosn(s1),s1.end());
string sub2(getNbrPosn(s2),s2.end());
return convert(sub1) > convert(sub2);
}
int main(){
vector<string> svec;
string temp;
while (getline(cin,temp))
svec.push_back(temp); //获取输入字符串
stable_sort(svec.begin(),svec.end(),moreThan); //按关键字排序
vector<string>::iterator p = svec.begin();
for (;p != svec.end();++p) //输出结果
cout << *p << (p+1 == svec.end() ? "\n" : " > ");
return 0;
}
[此贴子已经被作者于2007-6-11 22:00:03编辑过]