鱼C论坛

 找回密码
 立即注册
查看: 148|回复: 1

[已解决]还是有关自定义比较器使用问题

[复制链接]
发表于 2025-3-23 10:03:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面是“黑马程序员”map教学案例,请看过代码后(特别是封装的打印函数)再看下我遇到的问题:

首先不使用封装的函数,遍历map是正常的,但是当使用封装的函数(printMap)遍历时,提示错误,在传递参数和定义迭代器it时,无论带不带myCompare,都提示错误,因为本人刚学习到map章节,理解能力有限,特向大神求助。

#include <iostream>
#include <map>
using namespace std;

//void printMap(map<int, int,myCompare>& mp)
//{
//        for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
//        {
//                cout << "键= " << it->first << "  值= " << it->second << endl;
//        }
//        cout << endl;
//        cout << endl;
//}

class myCompare
{
public:
        bool operator()(int v1,int v2)const
        {
                return v1 > v2;
        }
};

void test1()
{
        map<int, int,myCompare> mp;
        mp.insert(make_pair(1, 30));
        mp.insert(make_pair(2, 20));
        mp.insert(make_pair(5, 50));
        mp.insert(make_pair(3, 40));
        mp.insert(make_pair(4, 70));

        //printMap(mp);
        for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
        {
                cout << "键= " << it->first << "  值= " << it->second << endl;
        }
        cout << endl;
}

int main()
{

        test1();

        system("pause");
        return 0;
}
最佳答案
2025-3-23 15:51:06
在 printMap 函数的定义中,myCompare 是一个未定义的标识符,因为它没有被正确传递到函数中。
try this:
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;

  4. class myCompare
  5. {
  6. public:
  7.     bool operator()(int v1, int v2) const
  8.     {
  9.         return v1 > v2;
  10.     }
  11. };

  12. // 修正后的printMap函数
  13. void printMap(const map<int, int, myCompare>& mp)
  14. {
  15.     for (map<int, int, myCompare>::const_iterator it = mp.begin(); it != mp.end(); it++)
  16.     {
  17.         cout << "键= " << it->first << "  值= " << it->second << endl;
  18.     }
  19.     cout << endl;
  20.     cout << endl;
  21. }

  22. void test1()
  23. {
  24.     map<int, int, myCompare> mp;
  25.     mp.insert(make_pair(1, 30));
  26.     mp.insert(make_pair(2, 20));
  27.     mp.insert(make_pair(5, 50));
  28.     mp.insert(make_pair(3, 40));
  29.     mp.insert(make_pair(4, 70));

  30.     printMap(mp);  // 使用封装的函数打印map

  31.     // 也可以直接遍历map
  32.     for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
  33.     {
  34.         cout << "键= " << it->first << "  值= " << it->second << endl;
  35.     }
  36.     cout << endl;
  37. }

  38. int main()
  39. {
  40.     test1();

  41.     system("pause");
  42.     return 0;
  43. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-23 15:51:06 | 显示全部楼层    本楼为最佳答案   
在 printMap 函数的定义中,myCompare 是一个未定义的标识符,因为它没有被正确传递到函数中。
try this:
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;

  4. class myCompare
  5. {
  6. public:
  7.     bool operator()(int v1, int v2) const
  8.     {
  9.         return v1 > v2;
  10.     }
  11. };

  12. // 修正后的printMap函数
  13. void printMap(const map<int, int, myCompare>& mp)
  14. {
  15.     for (map<int, int, myCompare>::const_iterator it = mp.begin(); it != mp.end(); it++)
  16.     {
  17.         cout << "键= " << it->first << "  值= " << it->second << endl;
  18.     }
  19.     cout << endl;
  20.     cout << endl;
  21. }

  22. void test1()
  23. {
  24.     map<int, int, myCompare> mp;
  25.     mp.insert(make_pair(1, 30));
  26.     mp.insert(make_pair(2, 20));
  27.     mp.insert(make_pair(5, 50));
  28.     mp.insert(make_pair(3, 40));
  29.     mp.insert(make_pair(4, 70));

  30.     printMap(mp);  // 使用封装的函数打印map

  31.     // 也可以直接遍历map
  32.     for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
  33.     {
  34.         cout << "键= " << it->first << "  值= " << it->second << endl;
  35.     }
  36.     cout << endl;
  37. }

  38. int main()
  39. {
  40.     test1();

  41.     system("pause");
  42.     return 0;
  43. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 02:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表