The implementation of the map prevents you from sorting a map by its values.
If you wish to emit the items in the map in this sorted order, you must first dump the contents of the map into a vector (say) and sort that vector:
template <typename T1, typename T2>
struct less_second {
typedef pair<T1, T2> type;
bool operator ()(type const& a, type const& b) const {
return a.second < b.second;
}
};
map<string, int> mymap;
// …
vector<pair<string, int> > mapcopy(mymap.begin(), mymap.end());
sort(mapcopy.begin(), mapcopy.end(), less_second<string, int>());
Alternatively, you can just copy the values from the map while leaving the keys alone and sort the resultant vector directly.