鱼C论坛

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

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

[复制链接]
发表于 5 天前 | 显示全部楼层 |阅读模式

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

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

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;
}
最佳答案
5 天前
在 printMap 函数的定义中,myCompare 是一个未定义的标识符,因为它没有被正确传递到函数中。
try this:
#include <iostream>
#include <map>
using namespace std;

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

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

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);  // 使用封装的函数打印map

    // 也可以直接遍历map
    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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

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);  // 使用封装的函数打印map

    // 也可以直接遍历map
    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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-3-28 06:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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