我正在尝试按降序对地图进行排序,但没有得到预期的结果?

I am trying to sort the map in decreasing order but i am not getting the desired result?

本文关键字:结果 排序 降序 地图      更新时间:2023-10-16
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef long long int ll;
int main()
{
string s;
cin >> s;
map<char, int,greater <int>> m;
m['A'] = 1;
m['C'] = 1;
m['G'] = 1;
m['T'] = 1;
for(ll i = 0; i < s.length()-1; i++)
{
if(s[i] == s[i+1])  //ATTCGGGA
m[s[i]]++;

}
for(auto it = m.begin(); it != m.end(); it++)
{
cout <<it->first <<" " <<it->second<<endl;
}
//cout <<it->second<<endl;
return 0;
}

我想要的输出应该是 G 3 美国 一 1 C 1 但它的显示 美国 G 3 C 1 一 1 我不知道为什么会发生这种情况,因为我已经提到它在有序地图中更大。 请解决该问题?

你可以简单地使用队列。 如果要使用地图来执行此操作,请使用 multi_map((;并使用 int 作为键,使用 char 作为值,这样您就可以根据 int 值对它们进行排序