我用了map,但是map不能用sort排序,所以我把它拷到vector,在排序,排序用的sort要是稳定的排序,不然他位置可能有交换。

#include <algorithm>
#include <sstream>
#include <map>
#include <functional>
#include <vector>


using namespace std;

int main() {
    int n;
    while(cin>>n){
        bool ascd=false;
        cin>>ascd;
        string str1,str2;
        multimap<int,string> m;
        for(int i =0;i<n;i++){
            istringstream s;
            cin>>str1;
            cin>>str2;
            m.insert(make_pair(stoi(str2), str1));
        }
        if(!ascd) {
            vector<pair<int,string>> v(m.begin(),m.end());
            stable_sort(v.begin(),v.end(),[](const pair<int,string> &a,
                                   const pair<int,string> &b){
                 return a.first>b.first;
            });
            auto it = v.begin();
            while(it!=v.end()){
                cout<<(*it).second<<' '<<(*it).first<<endl;
            it++;
            }
        }
        else{ 
            
        auto it = m.begin();
        while(it!=m.end()){
            cout<<(*it).second<<' '<<(*it).first<<endl;
            it++;
        }
        }
    }
}