map 如何使用结构体作为自定义键值

2023-11-16

在使用map时,有时候我们需要自定义键值,才能符合程序的需要。

比如我们需要使用自定义的结构体来作为map的键值:

struct  Test
{
    int x;
    int y;
};

这样直接使用的话,在编译时会出问题:

1>------ Build started: Project: MapRemove, Configuration: Debug Win32 ------
1>  Source.cpp
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::tuple<_Types...> &,const std::tuple<_Types1...> &)' : could not deduce template argument for 'const std::tuple<_Types...> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(480) : see declaration of 'std::operator <'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(192) : while compiling class template member function 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Kty=Test
1>  ,            _Ty=Test
1>          ]
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\map(228) : see reference to function template instantiation 'bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const' being compiled
1>          with
1>          [
1>              _Kty=Test
1>  ,            _Ty=Test
1>          ]
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::less<_Kty>' being compiled
1>          with
1>          [
1>              _Kty=Test
1>          ]
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1023) : see reference to class template instantiation 'std::is_empty<std::less<_Kty>>' being compiled
1>          with
1>          [
1>              _Kty=Test
1>          ]
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\map(70) : see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>' being compiled
1>          with
1>          [
1>              _Kty=Test
1>  ,            _Ty=std::string
1>  ,            _Pr=std::less<Test>
1>  ,            _Alloc=std::allocator<std::pair<const Test,std::string>>
1>          ]
1>          f:\xdd\xaudio2\mapremove\source.cpp(12) : see reference to class template instantiation 'std::map<Test,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
1>          with
1>          [
1>              _Kty=Test
1>  ,            _Ty=std::string
1>          ]
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(2259) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2545) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2535) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2525) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(1997) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(1155) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(971) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const Test'
1>          d:\program files (x86)\microsoft visual studio 12.0\vc\include\utility(230) : see declaration of 'std::operator <'
1>d:\program files (x86)\microsoft visual studio 12.0\vc\include\xstddef(193): error C2676: binary '<' : 'const Test' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

看错误是说,键值无法比较。因为map的键值是自动比较后进插入的,键值是递增的。

现在我们自定义的键值,编译器无法进行比较,找不到类似的模板,所以报错。

既然是没有‘<’,那我们自己重载小于操作符应该就可以了:

struct  Test
{
    int x;
    int y;

    bool operator < (const Test &o) const
    {
        return x < o.x || y < o.y;
    }
};

重载后,重新编译,顺利通过。测试代码如下:

#include <map>
#include <iostream>

struct  Test
{
    int x;
    int y;

    bool operator < (const Test &o) const
    {
        return x < o.x || y < o.y;
    }
};

int main()
{
    std::map<Test, std::string> mapTest;
    Test test = { 1, 2 };
    mapTest[test] = "Test1";

    for (auto it = mapTest.begin(); it != mapTest.end();it++)
    {
        std::cout << it->first.x << " " << it->first.y << " " << it->second.c_str() << std::endl;
    }

    return 0;
}

测试结果:

1 2 Test1

此文章转载自:https://blog.csdn.net/u011417605/article/details/50895097

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

map 如何使用结构体作为自定义键值 的相关文章

随机推荐

  • RPMB原理介绍

    RPMB介绍 RPMB Replay Protected Memory Block Partition 是 eMMC 中的一个具有安全特性的分区 eMMC 在写入数据到 RPMB 时 会校验数据的合法性 只有指定的 Host 才能够写入 同
  • matlab确定灰度阈值T,基于阈值的灰度图像提取法

    对于简单的灰度图像 如果目标与背景的灰度存在一定的差异 那么可以用阈值来提取目标 关键是确定阈值 常用方法有 模态法 P参数法 可变阈值法 大津法和迭代逼近法等 模态法 取直方图的波谷作为阈值 适用于目标与背景灰度差异较大 目标与背景的直方
  • 2021-05-27

    点三信息Java后端实习面经 长沙 1 你做这个rpc项目的动机是什么 我 为了学习 后来面试官又说我动机不明 醉了 2 讲一下BIO NIO AIO 不用AIO的话 发送过来一个包的流程是怎样的 3 零拷贝 什么情况下适用零拷贝 什么情况
  • 史上最全 55道 MySQL面试题及答案,看完碾压面试官

    前言 MySQL面试题以及答案整理 最新版 MySQL高级面试题大全 发现网上很多MySQL面试题都没有答案 所以花了很长时间搜集 本套MySQL面试题大全 汇总了大量经典的MySQL程序员面试题以及答案 包含MySQL语言常见面试题 My
  • 解决Transformer固有缺陷:复旦大学等提出线性复杂度SOFT

    作者 机器之心编辑部 来源 机器之心 来自复旦大学 萨里大学和华为诺亚方舟实验室的研究者首次提出一种无 softmax Transformer 视觉 Transformer ViT 借助 patch wise 图像标记化和自注意力机制已经在
  • 一个docker内不同参数执行个npm run

    dockerfile www wwwroot cat Dockerfile FROM node 12 18 0 alpine3 11 ARG NPM RUN ARG build ARG NPM RUN ARG TWO build story
  • qt序列帧读取

    描述 有时候ui给我们的动画是一序列的图片 播放动画需要一张一张图片读取 显得有点麻烦 存储的资源目录也显得比较凌乱 为解决这个问题 又不想使用gif 可以使用如下自定义序列帧播放组件 一次读取 通过计算当前帧的位置从内存读取当前帧 可以控
  • 027:vue中两列表数据联动,购物车添加、删除和状态更改

    第027个 查看专栏目录 VUE element UI 专栏目标 在vue和element UI联合技术栈的操控下 本专栏提供行之有效的源代码示例和信息点介绍 做到灵活运用 1 提供vue2的一些基本操作 安装 引用 模板使用 comput
  • 手机键盘字母组合

    题目 给定一个仅包含数字 2 9 的字符串 返回所有它能表示的字母组合 给出数字到字母的映射如下 与电话按键相同 注意 1 不对应任何字母 示例 输入 23 输出 ad ae af bd be bf cd ce cf 思路 根据输入的数字
  • 安卓逆向-IDA

    Java层代码是对系统层 so层 的封装 所以假如说APP开发者直接调用系统的接口而不是Java层 有些地方我们hook不出来 所以需要深入去了解so层的接口 IDA的使用 按F5可将汇编代码转C JNI OnLoad so层的hook接口
  • maria数据库随机查询_如何安装Maria DB,创建数据库和执行查询

    maria数据库随机查询 你好朋友 在本教程中 我们将看到 1 如何安装Maria DB 2 如何连接到Maria DB 创建数据库和执行查询 1 如何安装Maria DB 1 1 转到以下链接 然后单击 立即下载10 4 12稳定 按钮
  • JNPF低代码开发平台 新版3.4.6 框架源码分享

    每一个传统行业都有机会变成技术驱动的现代行业 这次疫情正在把很多原来的应急技术变成日常技术 数字化进程中最大的受益者不是互联网企业 而是用互联网改造自己的企业 独木不成林 或许JNPF快速开发平台正是意识到了这一点 才忙着织出一张密集的网
  • Python---多线程编程、基于Socket完成服务端程序开发、基于Socket完成客户端程序开发

    1 进程 程序在操作系统内运行 即成为一个运行进程 线程 进程内部可以有多个线程 程序的运行本质上就是由进程内部的线程在实际工作的 并行执行 多个进程同时在运行 即不同的程序同时运行 称之为 多任务并行执行 一个进程内的多个线程同时在运行
  • redis Hyperloglog 过滤重复

    Redis 中对 HyperLogLog 的应用 首先 在 Redis 中 HyperLogLog 是它的一种高级数据结构 提供有包含但不限于下面两条命令 pfadd key value 将 key 对应的一个 value 存入 pfcou
  • 【Linux】详解套接字编程

    文章目录 网络套接字 1 端口号 1 1认识端口号 1 2端口号VS PID 2 TCP与UDP协议 3 网络字节序 4 socket编程 4 1常用接口 4 2sockaddr结构 4 3 socket接口的底层工作 4 4字符串IP V
  • 计算机视觉、模式识别、机器学习常用牛人主页链接

    牛人主页 主页有很多论文代码 Serge Belongie at UC San Diego Antonio Torralba at MIT Alexei Ffros at CMU Ce Liu at Microsoft Research N
  • Python课堂笔记之输出数组中的数字

    将数组中的数字顺序输出
  • PLSQL中如何进行界面的基本使用和编辑。

    打开plsql后点击左上角的钥匙后可以看到最近登录的用户名和实例名称 System ORCL 其中system代表用户名 ORCL代表实例号 服务名 如何调整关键字的颜色 字体呢 选择Tools gt Preference gt 查看左侧栏
  • PAT乙级1023 组个最小数 (20 分)

    1023 组个最小数 20 分 一 问题描述 给定数字 0 9 各若干个 你可以以任意顺序排列这些数字 但必须全部使用 目标是使得最后得到的数尽可能小 注意 0 不能做首位 例如 给定两个 0 两个 1 三个 5 一个 8 我们得到的最小的
  • map 如何使用结构体作为自定义键值

    在使用map时 有时候我们需要自定义键值 才能符合程序的需要 比如我们需要使用自定义的结构体来作为map的键值 struct Test int x int y 这样直接使用的话 在编译时会出问题 1 gt Build started Pro