while (cin >> s) M[s]++; for (map<string,int>::iterator i = M.begin(); i != M.end(); i++) { S.insert(make_pair(i->second, i->first)); }