鱼C论坛

 找回密码
 立即注册
查看: 217|回复: 5

[已解决]自定义比较器在set中使用出错

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

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

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

x
本帖最后由 laocaiye 于 2025-3-24 22:04 编辑

下面这段代码编译或运行时怎么老是提示错误

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

class MyCompare
{
        bool operator()(const int &v1,const int &v2)
        {
                return v1 > v2;
        }
};


void test4()
{
        set<int, MyCompare>st;
        st.insert(10);
        st.insert(70);
        st.insert(30);
        st.insert(80);
        st.insert(20);
        for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
        {
                cout << *it << " ";
        }
        cout << endl;

}

int main()
{
        test4();

        system("pause");
        return 0;
}
最佳答案
2025-3-21 23:24:06
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;

  4. class MyCompare
  5. {
  6. public:  // 这里需要添加public访问修饰符
  7.         bool operator()(const int &v1,const int &v2)
  8.         {
  9.                 return v1 > v2;
  10.         }
  11. };


  12. void test4()
  13. {
  14.         set<int, MyCompare>st;
  15.         st.insert(10);
  16.         st.insert(70);
  17.         st.insert(30);
  18.         st.insert(80);
  19.         st.insert(20);
  20.         for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
  21.         {
  22.                 cout << *it << " ";
  23.         }
  24.         cout << endl;

  25. }

  26. int main()
  27. {
  28.         test4();

  29.         system("pause");
  30.         return 0;
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-21 23:20:27 | 显示全部楼层
你的代码在 `set` 中使用自定义比较器时出错,主要有以下几个问题:  

1. **`MyCompare` 的 `operator()` 需要是 `public`**  
   你的 `MyCompare` 类中的 `operator()` 是 `private`,默认情况下,类的成员函数是 `private`,所以 `set` 无法访问它。你需要将 `operator()` 设为 `public`。  

2. **`set` 需要指定比较器类型**  
   你在 `set` 的定义中没有正确指定 `MyCompare` 作为比较器。`set` 需要使用 `set<int, MyCompare>` 这样的格式。  

### **修改后的代码**

  1. #include <iostream>
  2. #include <set>

  3. using namespace std;

  4. class MyCompare
  5. {
  6. public:
  7.     bool operator()(const int &v1, const int &v2) const
  8.     {
  9.         return v1 > v2; // 降序排列
  10.     }
  11. };

  12. void test4()
  13. {
  14.     set<int, MyCompare> st; // 指定 MyCompare 作为比较器
  15.     st.insert(10);
  16.     st.insert(70);
  17.     st.insert(30);
  18.     st.insert(80);
  19.     st.insert(20);

  20.     for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
  21.     {
  22.         cout << *it << " ";
  23.     }
  24.     cout << endl;
  25. }

  26. int main()
  27. {
  28.     test4();
  29.     return 0;
  30. }
复制代码


### **修正点**
1. **`operator()` 设为 `public`**,否则 `set` 不能访问它。  
2. **`operator()` 需要加 `const`**,因为 `set` 需要一个**可调用的比较器**,而 `operator()` 不能修改 `MyCompare` 的状态。  
3. **`set<int, MyCompare>`**,正确指定 `MyCompare` 作为 `set` 的比较器。  

### **运行结果**
这段代码会按照**降序**(从大到小)排序并输出:
  1. 80 70 30 20 10
复制代码


这样就可以正确使用 `set` 了!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-21 23:22:26 | 显示全部楼层
加上Public也不行,尝试修改为结构体也不行,这个public就是从结构体改回class时,忘记加上了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-3-21 23:22:42 | 显示全部楼层
laocaiye 发表于 2025-03-21 23:22
加上Public也不行,尝试修改为结构体也不行,这个publ ...


你的代码仍然有一些问题,主要是以下几点:

1. **缺少头文件**  
   你没有包含 `<set>` 和 `<iostream>` 头文件,导致 `set` 和 `cout` 无法识别。

2. **比较器 `operator()` 需要 `const`**  
   在 `set` 中,自定义比较器的 `operator()` 需要声明为 `const`,否则 `set` 可能无法正确调用它。

3. **`set` 需要指定比较器类型**  
   你在 `set` 的定义中没有正确指定 `MyCompare` 作为比较器。

### **修改后的代码**
  1. #include <iostream>
  2. #include <set>

  3. using namespace std;

  4. class MyCompare
  5. {
  6. public:
  7.     bool operator()(const int &v1, const int &v2) const
  8.     {
  9.         return v1 > v2; // 降序排列
  10.     }
  11. };

  12. void test4()
  13. {
  14.     set<int, MyCompare> st; // 指定 MyCompare 作为比较器
  15.     st.insert(10);
  16.     st.insert(70);
  17.     st.insert(30);
  18.     st.insert(80);
  19.     st.insert(20);

  20.     for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
  21.     {
  22.         cout << *it << " ";
  23.     }
  24.     cout << endl;
  25. }

  26. int main()
  27. {
  28.     test4();
  29.     return 0;
  30. }
复制代码


### **关键修改点**
1. **添加了 `<set>` 和 `<iostream>` 头文件**。
2. **在 `operator()` 末尾添加 `const`**,确保 `set` 可以正确调用它。
3. **在 `set<int, MyCompare>` 中指定 `MyCompare` 作为比较器**。

这样修改后,代码应该可以正确编译和运行,并按照降序(`80 70 30 20 10`)输出 `set` 中的元素。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-3-21 23:24:06 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;

  4. class MyCompare
  5. {
  6. public:  // 这里需要添加public访问修饰符
  7.         bool operator()(const int &v1,const int &v2)
  8.         {
  9.                 return v1 > v2;
  10.         }
  11. };


  12. void test4()
  13. {
  14.         set<int, MyCompare>st;
  15.         st.insert(10);
  16.         st.insert(70);
  17.         st.insert(30);
  18.         st.insert(80);
  19.         st.insert(20);
  20.         for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
  21.         {
  22.                 cout << *it << " ";
  23.         }
  24.         cout << endl;

  25. }

  26. int main()
  27. {
  28.         test4();

  29.         system("pause");
  30.         return 0;
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-21 23:25:45 | 显示全部楼层
这是从黑马程序员视频教学中抄过来的,视频教学中,在仿函数后也没有const,但是运行正常,怎么到我这里就不行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 00:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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