C++ 如何对地图进行排序<;int,pair<;int,int>&燃气轮机;仅根据第二个元素?

C++ 如何对地图进行排序<;int,pair<;int,int>&燃气轮机;仅根据第二个元素?,c++,stl,c++14,C++,Stl,C++14,我已经创建了Cmp()函数用作比较器。但是我得到了一个错误。 我已经编写了此代码。它显示错误: #include<bits/stdc++.h> using namespace std; struct cmp { bool operator() (multimap<int,pair<int,int> > a, multimap<int,pair<int,int> > b) {

我已经创建了Cmp()函数用作比较器。但是我得到了一个错误。 我已经编写了此代码。它显示错误:

#include<bits/stdc++.h>
using namespace std;
struct cmp
    {
        bool operator() (multimap<int,pair<int,int> > a, multimap<int,pair<int,int> > b)
            {
                if(a->second.second>b->second.second)
                    return 1;
                return 0;
            }
    };
int main()
{
    std::ios::sync_with_stdio(false);
    int test,i;
    long long sum=0;
    cin>>test;
    while(test--)
    {
        multimap<int, pair<int,int>,cmp > mymap;
        multimap<int, pair<int,int> >::iterator it;
        int n,days,d,t,s;
        cin>>n>>days;
        for(i=0;i<n;i++)
        {
            cin>>d>>t>>s;
            mymap.insert(make_pair(d,make_pair(t,s)));
        }
        for(it=mymap.begin();it!=mymap.end();it++)
        {
            cout<<it->first<<"  "<<it->second.first<<" "<<it->second.second<<endl;
        }

    }
    return 0;
}
#包括
使用名称空间std;
结构cmp
{
布尔运算符()(多重映射a、多重映射b)
{
如果(a->second.second>b->second.second)
返回1;
返回0;
}
};
int main()
{
std::ios::与stdio同步(false);
int检验,i;
长和=0;
cin>>试验;
而(测试--)
{
多重映射mymap;
多重映射::迭代器;
整数n,天,d,t,s;
cin>>n>>天;
对于(i=0;i>d>>t>>s;
插入(make_pair(d,make_pair(t,s));
}
for(it=mymap.begin();it!=mymap.end();it++)
{

cout这个问题的基础毫无意义。来源:

键比较等效的键-值对的顺序为插入顺序,不会更改

您不能指定值的顺序。如果需要对它们进行排序,您需要首先将它们复制到新容器中,或者首先将它们放入新容器中


另外,
Compare
类型比较键,而不是整个映射。链接引用中也有一些示例。

您不能按值对映射进行排序。可能您正在寻找一个
std::set
?您的意思是反转键和值吗?您没有传递到函数
操作符()
按指针,但按值。所以像
a->second.second这样的寻址不起作用。你为什么不试着按引用传递呢。好的..我选择std::set…谢谢
是(a)不是标准文件,和(b)吗这是一种不好的做法,因为使用它您无法了解标准库中各个部分的单独包含。请参阅
 [Error] base operand of '->' has non-pointer type 'std::multimap<int, 
std::pair<int, int> >'
eg:- suppose i have

(3,(2,300))
(3,(1,400))
(3,(2,500))
(2,(3,100))
(2,(2,500))
(1,(5,100))

I want output like this:
(1,(5,100))
(2,(2,500))
(2,(3,100))
(3,(2,500))
(3,(1,400))
(3,(2,300))

   Only the second element of pair<int,int> sorted decreasingly.