#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
struct student_score {
char name[16];
int score;
};
int cmp(student_score a, student_score b)
{
return a.score > b.score;
}
int main()
{
map<string , int> ScoreMap = {
{"卢珍", 82}, {"童云", 88}, {"童贞", 71},
{"蒋成", 68}, {"姚伟", 98}, {"谢建国", 78}
}; // C++ 11X 容器方便初始化 和 auto遍历
list<student_score> ScoreList;
student_score tmp;
cout << string(30, '_') << " 建立MAP时的排序 " << string(30, '_') << endl;
for (auto it = ScoreMap.begin(); it != ScoreMap.end() ; ++it) {
cout << it->first << "\t=> " << it->second << endl;
strcpy(tmp.name, it->first.c_str());
tmp.score = it->second;
ScoreList.push_back(tmp);
}
cout << string(30, '_') << " 按成绩降序排序 " << string(30, '_') << endl;
ScoreList.sort(cmp);
for (auto it = ScoreList.begin(); it != ScoreList.end() ; ++it) {
cout << it->name << "\t=> " << it->score << endl;
}
cout << string(30, '_') << " 反转排序 " << string(30, '_') << endl;
ScoreList.reverse();
for (auto it = ScoreList.begin(); it != ScoreList.end() ; ++it) {
cout << it->name << "\t=> " << it->score << endl;
}
return 0;
}
引用 http://blog.youkuaiyun.com/hongwenjun/article/details/7359720