# STL地图 – >按价值sorting？

``map<int, int>; m[1] = 10; m[2] = 5; m[4] = 6; m[6] = 1;` `

` `m[6] = 1 m[2] = 5 m[4] = 6 m[1] = 10` `

` `int main() { string s; map<string, int> counters; while(cin >> s) ++counters[s]; //Get the largest and smallest values from map int beginPos = smallest_map_value(counters); int endPos = largest_map_value(counters); //Increment through smallest value to largest values found for(int i = beginPos; i <= endPos; ++i) { //For each increment, go through the map... for(map<string, int>::const_iterator it = counters.begin(); it != counters.end(); ++it) { //...and print out any pairs with matching values if(it->second == i) { cout << it->first << "\t" << it->second << endl; } } } return 0; } //Find the smallest value for a map<string, int> int smallest_map_value(const map<string, int>& m) { map<string, int>::const_iterator it = m.begin(); int lowest = it->second; for(map<string, int>::const_iterator it = m.begin(); it != m.end(); ++it) { if(it->second < lowest) lowest = it->second; } return lowest; } //Find the largest value for a map<string, int> int largest_map_value(const map<string, int>& m) { map<string, int>::const_iterator it = m.begin(); int highest = it->second; for(map<string, int>::const_iterator it = m.begin(); it != m.end(); ++it) { if(it->second > highest) highest = it->second; } return highest; }` `