你如何在地图中排序?
我已经尝试了排序(myMap.begin(),myMap.end()),但它不起作用,我已经想在互联网上找到答案,但没有网站解释如何使用排序专门用于地图.和bdw我的地图有整数和它们的字母表示.
int main(){
map<int,string> m;
// add integers at the end of the map
m.insert(pair<int,string>(2,"Two"));
m.insert(pair<int,string>(3,"Three"));
m.insert(pair<int,string>(4,"Four"));
m.insert(pair<int,string>(7,"Seven"));
m.insert(pair<int,string>(5,"Five"));
cout<<"Finding minimum value from map"<<endl;
map<int,string>::const_iterator it3;
it3 = min_element(m.begin(),m.end());
cout<<"The minimum value in the vector is "<<it3->first<<","<<it3->second<<endl;
cout<<endl<<endl;
cout<<"Sorting array.."<<endl;
sort(m.begin(),m.end());
cout<<"Array after sorting.."<<endl;
cout << "The size of v is: " << m.size() << "\nThe capacity of v is: ";
cout<<m.max_size() <<endl;
cout<<"The content of v is:";
for (it3 = m.begin(); it3 != m.end(); it3++){
cout << it3->first << "," << it3->second << endl;
}
cout<<endl<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |