标签:
在项目当中有要用到map按key排序的需求,就在百度上搜了一下:
typedef pair<int,int> PAIR; int cmp(const PAIR& x, const PAIR& y) { return x.second < y.second; } map<int,int> imgdis; //待排序对象,根据double值排序 imgdis[1] = 3; imgdis[10] = 1; imgdis[3] = 5; imgdis[12] = 4; imgdis[5] = 4; vector<PAIR> vecpair; for (map<int,int>::iterator curr = imgdis.begin(); curr != imgdis.end(); ++curr) { vecpair.push_back(make_pair(curr->first, curr->second)); } sort(vecpair.begin(), vecpair.end(), cmp); //输出排序后的键值对 for(int i = 0 ;i<vecpair.size();i++) { cout<<" "<<vecpair[i].first<<" "<<vecpair[i].second<<endl; }
有更好的方法,欢迎推荐啊
标签:
原文地址:http://www.cnblogs.com/aj007/p/4211913.html