尝试将 std::pair 插入 std::set

2024-03-09

我无法理解这段代码中的错误是什么:

#include <set>
#include <utility>
#include <iostream>

using namespace std;

class A
{
    public:
        A(unsigned int a) : _a(a) { }
        A() : _a(0) { }
        unsigned int a() const { return _a; }
    private:
        unsigned int _a;
};

class B
{
    public:
        B(unsigned int b) : _b(b) { }
        B() : _b(0) { }
        unsigned int b() const { return _b; }
    private:
        unsigned int _b;
};

void display(const Point& point)
{
    //cout << "A: " << point.first.a() << ", B: " << point.second.b() << endl;
}

typedef pair <A, B> Point;
typedef set <Point> List;

main()
{
    A a(5);
    B b(9);

    List list;
    List::iterator it;
    Point point;

    point = make_pair(a, b);

    it = list.begin();

    list.insert(point); // <--- error here

    //display(point);
}

错误是这样的:

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_algobase.h:66,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_tree.h:62,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/set:60,
                 from test.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = A, _T2 = B]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_function.h:230:   instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<A, B>]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_tree.h:1170:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = std::pair<A, B>, _Val = std::pair<A, B>, _KeyOfValue = std::_Identity<std::pair<A, B> >, _Compare = std::less<std::pair<A, B> >, _Alloc = std::allocator<std::pair<A, B> >]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_set.h:411:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::pair<A, B>, _Compare = std::less<std::pair<A, B> >, _Alloc = std::allocator<std::pair<A, B> >]’
test.cpp:48:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__x->std::pair<A, B>::second < __y->std::pair<A, B>::second’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__y->std::pair<A, B>::first < __x->std::pair<A, B>::first’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4/bits/stl_pair.h:154: error: no match for ‘operator<’ in ‘__x->std::pair<A, B>::first < __y->std::pair<A, B>::first’

You are trying to use std::set with an element type that does not have ordering (std::pair), while a set needs that its elements have "a specific strict weak ordering criterion".


Update

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试将 std::pair 插入 std::set 的相关文章

随机推荐

  • 像 RStudio 这样的 Python 工作流程?

    我使用 Python 作为主要编程语言 有时会切换到 R 来执行快速数据分析任务 因为它具有用于统计编程目的的庞大库 尽管pandas http pandas pydata org 对于 python 来说太棒了 我猜大多数 R 用户都会选
  • 无法在 Android 模拟器中模拟位置数据

    我正在尝试测试我的应用程序 它使用模拟器使用地理定位 介绍了几种方法here https stackoverflow com questions 2279647 how to emulate gps location in the andr
  • 如何在数字数组中找到最佳匹配元素?

    我需要帮助解决一些看似简单但让我困惑的事情 尝试编写一些模糊匹配方法来处理根据需要计算的值与选择列表中实际可用的值之间的格式差异 价值 期权执行价格 始终是计算出的 Float 例如85 0 or Int 该数组包含字符串形式的数字 无论是
  • Jackson deearlization:根上有两个键。我如何打开其中一个并忽略另一个?

    使用杰克逊 2 x json 响应如下所示 flag true important id 123 email email protected cdn cgi l email protection flag 键不提供任何有用的信息 我想忽略
  • 为什么 C++ 编译器在创建机器代码之前将源代码翻译成汇编? [复制]

    这个问题在这里已经有答案了 我开始学习C 了解到编译器通过编译将源代码从程序变成机器代码 然而 我了解到 C 编译器实际上将源代码转换为汇编代码 作为将汇编代码转换为机器代码之前的中间步骤 这一步的目的是什么 他们为什么不直接将其翻译成机器
  • Reactjs/Apollo/AppSync 突变触发两次

    我遇到了 React Apollo AppSync 的问题 突变触发了两次 或多次 我有一个 React 应用程序 它有一个由通常的 UI 按钮 onClick 触发的更新突变
  • VS2012 发布网站 dll 而不是 cs 文件

    我有一个想要发布的简单 ASP NET 项目 我右键单击该项目并按 发布网站 但这会将我的所有 cs 文件生成到所选文件夹中 之前我一直在 bin 文件夹中获取 dll 文件而不是 cs 文件 我究竟做错了什么 问题可能出在发布设置中 请检
  • Javascript:检测 OS X“自然滚动”设置

    我面临一个问题 对于我正在做的项目 我正在检测滚轮位置 并根据该位置是否导航到下一张幻灯片 然而 一个问题是 一些 Mac 用户使用 自然滚动 反转页面上的滚动 这意味着 对于这些用户 我应该使用另一个方向的滚动作为触发器 我的问题是 有没
  • 在 VB.NET 中将 ArrayList 数据绑定到 ListBox?

    我在 VB NET 工作 我有一个名为 Invoices 的 ArrayList 其中填充了 Invoice 类的对象 我想将其数据绑定到 ListBox 以便随着 ArrayList 的内容更新并更改 ListBox 的更新 我已经在 I
  • 如何使用shell脚本访问mysql数据库?

    有没有办法使用 shell 脚本访问 MySQL 数据库 想要对多个表进行一些选择和插入 如果您能提供一些示例代码 那就太好了 因为我是脚本编写新手 这个链接似乎有你想要的信息 http www cyberciti biz faq usin
  • Android SQLite 中的多线程怎么样?

    在我的应用程序中 我必须实现一些 UI 和同步服务 它在后台运行并更新数据 同步服务并不是很简单 它使用了多线程 所以 这是我的故事 当我开始开发这个应用程序时 我对sqlite一无所知 所以我只是没有在Java中使用任何线程同步 结果 我
  • Qt 我可以在构造函数中将信号/槽连接到自身吗?

    编辑 与信号 插槽 连接无关 问题是构造函数调用构造函数 可能有更好的方法来做到这一点 我有兴趣听到这些 我有从 QLabel 派生的 MyClass 我想在信号中传递更多有关派生类的数据 而不是基本信号的数据 因此 我创建了一个插槽来拦截
  • 元音计数顺序

    这不是一个家庭作业问题 而是一个考试准备问题 我应该定义一个函数syllables word 计算音节数 一句话如下 元音的最大序列是一个音节 最终e在一个单词中不是一个音节 或者它是元音序列的一部分 的 我不必处理任何特殊情况 例如最终的
  • SQL - 将每个单词的第一个字母大写

    我知道这个线程到处都存在 但是 这是一个略有不同的情况 在我公司使用的套件中 我对 SQL 的访问权限有限 无法运行具有函数等的复杂代码 我有一个 SQL 查询 将多个列上的数据编译为一列 并使用 group by 子句来消除多重性 然而
  • TSQL RIGHT 字符串函数不起作用

    我无法理解为什么正确的功能不适合我 我试图在这里容纳尽可能多的输出 如果阅读起来令人困惑 我深表歉意 DECLARE Nbr VARCHAR 27 SELECT Nbr xmz nbr FROM xml temp AS xmz SELECT
  • 要显示此页面,Firefox 必须发送将重复之前执行的任何操作(例如搜索或订单确认)的信息

    嘿 我收到了 Firefox 的确认 To display this page Firefox must send information that will repeat any action such as a search or or
  • Cargo 是否可以在不构建应用程序的情况下下载并构建依赖项?

    有没有办法告诉Cargo http doc crates io guide html安装和构建我的所有依赖项 但不尝试构建我的应用程序 我想cargo install会这样做 但实际上它也一直用于构建我的应用程序 我想要达到一个状态carg
  • 如何从 Google Assistant 接收答案作为字符串,而不是音频流

    我正在使用 Assistant SDK 中的 python 库通过 gRPC 进行语音识别 我已识别语音并以调用该方法的字符串形式返回resp result spoken request text from googlesamples as
  • 检测 Skype 是否处于“紧凑视图”或“默认视图”

    我的应用程序的运行方式由 Skype 的视图模式决定 因为我的应用程序正在寻找类窗口TConversationWindow 如果在默认视图中 它是tSkMainForm 如果在紧凑视图中 它不是tSkMainForm 这是我尝试做的 Fun
  • 尝试将 std::pair 插入 std::set

    我无法理解这段代码中的错误是什么 include