Heaven HXF 2021-10-19 15:36 采纳率: 0%
浏览 13

C++map容器使用出现报错,不知道什么问题

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
void printMap(map<int,int>& m)
{
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key = " << it->first << "value = " << it->second;
        cout << endl;
    }
    cout << endl;
}
class MyCompare
{
public:
    bool operator ()(int v1, int v2)const
    {
        return v1 > v2;
    }
};
void test02()
{
    map<const int, const int, MyCompare> m;
    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(3, 30));
    m.insert(make_pair(4, 40));
    m.insert(make_pair(5, 50));
    printMap(m);
}
int main()
{
    test02();
}

这段在printMap(m)时会报错,不清楚为什么

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-10-19 15:50
    关注
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<map>
    using namespace std;
    
    class MyCompare
    {
    public:
        bool operator ()(int v1, int v2)const
        {
            return v1 > v2;
        }
    };
    
    void printMap(map<int,int,MyCompare>& m)
    {
        for (map<int, int,MyCompare>::iterator it = m.begin(); it != m.end(); it++)
        {
            cout << "key = " << it->first << "value = " << it->second;
            cout << endl;
        }
        cout << endl;
    }
    
    void test02()
    {
        map<int, int, MyCompare> m;
        m.insert(make_pair(1, 10));
        m.insert(make_pair(2, 20));
        m.insert(make_pair(3, 30));
        m.insert(make_pair(4, 40));
        m.insert(make_pair(5, 50));
        printMap(m);
    }
    int main()
    {
        test02();
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月19日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真