c++ STL map 结构体

2023-10-31

点击打开链接

点击打开链接

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题

第一种:小于号重载,程序举例

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

}StudentInfo, *PStudentInfo;  //学生信息

 

Int main()

{

    int nSize;

       //用学生信息映射分数

       map<StudentInfo, int>mapStudent;

    map<StudentInfo, int>::iterator iter;

       StudentInfo studentInfo;

       studentInfo.nID = 1;

       studentInfo.strName = “student_one”;

       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

       studentInfo.nID = 2;

       studentInfo.strName = “student_two”;

mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

 

for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)

    cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;

 

}

以上程序是无法编译通过的,只要重载小于号,就OK了,如下:

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

       Bool operator < (tagStudentInfo const& _A) const

       {

              //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序

              If(nID < _A.nID)  return true;

              If(nID == _A.nID) return strName.compare(_A.strName) < 0;

              Return false;

       }

}StudentInfo, *PStudentInfo;  //学生信息

第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明

#include <map>

#include <string>

Using namespace std;

Typedef struct tagStudentInfo

{

       Int      nID;

       String   strName;

}StudentInfo, *PStudentInfo;  //学生信息

 

Classs sort

{

       Public:

       Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const

       {

              If(_A.nID < _B.nID) return true;

              If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;

              Return false;

       }

};

 

Int main()

{

       //用学生信息映射分数

       Map<StudentInfo, int, sort>mapStudent;

       StudentInfo studentInfo;

       studentInfo.nID = 1;

       studentInfo.strName = “student_one”;

       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

       studentInfo.nID = 2;

       studentInfo.strName = “student_two”;

mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

}

  1. /****************************************************************** 
  2.       map的基本操作函数: 
  3.       C++ Maps是一种关联式容器,包含“关键字/值”对 
  4.       begin()          返回指向map头部的迭代器 
  5.       clear()         删除所有元素 
  6.       count()          返回指定元素出现的次数 
  7.       empty()          如果map为空则返回true 
  8.       end()            返回指向map末尾的迭代器 
  9.       equal_range()    返回特殊条目的迭代器对 
  10.       erase()          删除一个元素 
  11.       find()           查找一个元素 
  12.       get_allocator()  返回map的配置器 
  13.       insert()         插入元素 
  14.       key_comp()       返回比较元素key的函数 
  15.       lower_bound()    返回键值>=给定元素的第一个位置 
  16.       max_size()       返回可以容纳的最大元素个数 
  17.       rbegin()         返回一个指向map尾部的逆向迭代器 
  18.       rend()           返回一个指向map头部的逆向迭代器 
  19.       size()           返回map中元素的个数 
  20.       swap()            交换两个map 
  21.       upper_bound()     返回键值>给定元素的第一个位置 
  22.       value_comp()      返回比较元素value的函数
  1. ==================================================================== 
  2. 1、map构造 
  3.     map<int, string> mapStudent; 
  4.  
  5. 2、map添加数据 
  6.     mapStudent.insert(pair<int, string>(1, "student_one")); 
  7.     mapStudent.insert(map<int, string>::value_type(2, "student_two")); 
  8.     mapStudent[3] = "student_three"; 
  9.  
  10. ********************************************************************/  
  11. #pragma warning (disable:4786)  
  12. #include <map>  
  13. #include <string>  
  14. #include <iostream>  
  15.   
  16. using namespace std;  
  17.   
  18. int main()  
  19. {  
  20.     map<int, string> mapStudent;  
  21.     cout<<"三种插入方式:"<<endl;  
  22.   
  23.     mapStudent.insert(pair<int, string>(1, "student_one"));  
  24.     mapStudent.insert(map<int, string>::value_type(2, "student_two"));  
  25.     mapStudent[3] = "student_three";  
  26.     mapStudent.insert(map<int, string>::value_type(4, "student_four"));     
  27.       
  28.     pair<map<int,string>::iterator,bool> InsertPair;   //判断是否插入成功  
  29.     InsertPair = mapStudent.insert(map<int,string>::value_type(5,"student_five"));  
  30.     if(InsertPair.second == true)  
  31.     {  
  32.         //cout<<InsertPair.first.operator++<<endl;  //求解??不知道怎么应用第一个数据  
  33.     }  
  34.   
  35.     cout<<"三种遍历方式:"<<endl;  
  36.   
  37.     map<int, string>::iterator  iter;  
  38.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  39.     {  
  40.         cout<<iter->first<<" "<<iter->second<<endl;  
  41.     }  
  42.   
  43.     map<int, string>::reverse_iterator  iters;  
  44.     for(iters = mapStudent.rbegin(); iters != mapStudent.rend(); iters++)  
  45.     {  
  46.         cout<<iters->first<<" "<<iters->second<<endl;  
  47.     }  //逆序输出  
  48.     cout<<"数组的输出形式:"<<endl;  
  49.     for(int iIndex=0;iIndex < mapStudent.size();iIndex++) //size()返回成员的个数  
  50.     {  
  51.         cout<<mapStudent[iIndex]<<endl;  
  52.     }  
  53.     cout<<mapStudent.count(1)<<endl;  //count()判断关键字是否存在,返回1表示存在,0  
  54.     iter = mapStudent.find(1);        //find()关键字存在时,返回数据所在位置的迭代器,否则返回end()返回的迭代器  
  55.     if( iter != mapStudent.end() )  
  56.     {  
  57.         cout<<"数据存在:"<<iter->first<<" "<<iter->second<<endl;  
  58.         mapStudent.erase(iter);        //用迭代删除数据  
  59.     }  
  60.     else  
  61.     {  
  62.         cout<<"数据不存在!"<<endl;  
  63.     }  
  64.     int n = mapStudent.erase(3);        //用关键字删除,如果删除了会返回1,否则返回0  
  65.   
  66.     iter = mapStudent.lower_bound(2);   //返回2的迭代器  
  67.     cout<<iter->second<<endl;  
  68.     iter = mapStudent.upper_bound(2);   //返回3的迭代器  
  69.     cout<<iter->second<<endl;      
  70.   
  71.     /*Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字*/  
  72.     pair<map<int,string>::iterator,map<int,string>::iterator> MapPair;  
  73.     MapPair = mapStudent.equal_range(2);  
  74.     if( MapPair.first == MapPair.second )  
  75.     {  
  76.         cout<<"Do not find"<<endl;  
  77.     }  
  78.     else  
  79.     {  
  80.         cout<<"Find"<<endl;  
  81.     }  
  82.   
  83.     //删除一个前闭后开的集合,这是STL的特性  
  84.     mapStudent.earse(mapStudent.begin(), mapStudent.end());      
  85. }

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

c++ STL map 结构体 的相关文章

随机推荐

  • AIGC从入门到精通

    目录 1 概述 2 一键起飞 3 保持ID生成 4 教程 原理 训练阶段 采样阶段 5 大模型微调 6 训练 7 商业价值 Fooocus sd webui 界面 新手建议使用 ComfyUI 简体中文版 1 概述 Stable Diffu
  • [1065]impala查询内存限制Memory limit exceeded

    错误信息 ERROR Memory limit exceeded Query did not have enough memory to get the minimum required buffers in the block manag
  • Python3发送邮件

    coding utf 8 author ChenBaijing date 2022 4 7 14 33 Python对SMTP支持有smtplib和email两个模块 smtplib负责 登录邮件服务器 认证 和 发送邮件 对smtp协议进
  • vmware虚拟机开机黑屏的解决方法

    今天有一个项目需要用到OSX坯境 打开vmware 启动原来安装的 OSX10 11 结果启动后 无轮怎么操作就是黑屏 然后就百度了一下vmware黑屏的解决办法 尝试了一下问题解决 同时也把解决过程记录一下 1 挂起的时候我们是能够见到显
  • IPSec/GRE与PPTP的比较

    PPTP PPTP Point to Point Tunneling Protocol 是点对点的协议 基于拨号使用的PPP协议使用PAP或CHAP之类的加密算法 或者使用Microsoft的点对点加密算法MPPE 而L2TP Layer
  • Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候 我们需要用到动态数据源的情况 比如多租户的场景 系统登录时需要根据用户信息切换到用户对应的数据库 又比如业务A要访问A数据库 业务B要访问B数据库等 都可以使用动态数据源方案进行解决 接下来 我们就来讲
  • Ultra Libraian介绍

    Ultra Libraian介绍 我从Ultra Libraian官网上查找一些信息 在下面统一介绍一下它 方便大家使用 Ultra Libraian简介 Ultra Libraian是一个零件库服务商 提供方便电子工程师和PCB Layo
  • 边界值法中的上点、内点和离点分析

    1 边界值法概念 边界值法设计测试用例 是对输入或输出的边界值 有效等价类和无效等价类的界限 进行测试的一种黑盒测试方法 2 边界值法存在的意义 测试经验表明错误往往会发生在输入或输出范围的边界上 所以边界值法是对这些边界进行测试 是对划分
  • 多系统U盘启动盘的制作,成功启动win8PE,ubuntu,deepin2013,deepin2014,以及通过U盘启动电脑已装系统。

    以前的用U盘装系统都是用ultraISO 直接制作启动盘 有的时候一连着好几天都得捣鼓着装系统 今天是windows 明天是ubuntu 后天就可能是其它linux发行版了 很不方便 所以就想利用一个U盘做一个多系统的启动盘 经过N天不断的
  • Linux安装完mysql远程连接Authentication plugin

    root iZ2ze8bpfv23icsz3g2hp2Z log mysql u root p Enter password Welcome to the MySQL monitor Commands end with or g Your
  • MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数

    目录 upsample 和 downsample 程序验证 上采样 upsample 下采样 downsample 其他参数设定 总结 upsample 和 downsample upsample 和 downsample 顾名思义就是上采
  • Concis组件库封装——Loading加载中

    您好 如果喜欢我的文章 可以关注我的公众号 量子前端 将不定期关注推送前端好文 组件介绍 Loading组件是日常开发用的很多的组件 这次封装主要包含两种状态的Loading 旋转 省略号 话不多说先看一下组件的文档页面吧 Loading
  • Docker--简介与实践

    一 什么是容器技术 Docker 是一个开源的应用容器引擎 要了解Docker的前提就是要了解什么是容器技术 说到容器技术 这里就要联系一下我们经常使用到的虚拟机中的虚拟化技术了 两者在功能 目的上相似 都是将一系列的程序进行打包 建立一个
  • [系统安全] 四十二.Powershell恶意代码检测系列 (4)论文总结及抽象语法树(AST)提取

    您可能之前看到过我写的类似文章 为什么还要重复撰写呢 只是想更好地帮助初学者了解病毒逆向分析和系统安全 更加成体系且不破坏之前的系列 因此 我重新开设了这个专栏 准备系统整理和深入学习系统安全 逆向分析和恶意代码检测 系统安全 系列文章会更
  • 如何正确使用线程池

    具体请参考原创 Java线程池实现原理及其在美团业务中的实践 Java 线程池及参数动态调节详解 一 为何要使用线程池 降低资源消耗 线程的创建和销毁会造成一定的时间和空间上的消耗 线程池可以让我们重复利用已创建的线程 提高响应速度 线程池
  • Deform软件无法启动

    启动时卡在这个界面然后闪退 打开 提示 许可证被禁止了 点击star
  • [转]unity--角度调整和摄像头位置调整

    我之前在使用unity的时候大量的时间花在了把物体拉近镜头 把场景角度和摄像机角度调到一致上 这里总结下怎么做会比较快 关于物体的位移和缩放场景操作 右上角的标志是表示当前xyz轴方向的 为了方便调整 我们统一调成z轴朝上 y轴向前 x轴向
  • 如何实现浮点数立方根?

    给一个浮点数num 如何求其立方根ans 首先 0 lt ans lt num 对于浮点数区间的海量数据 若采用加法枚举判断 那绝对把CPU能累死 计算精度越高 时间复杂度越高 上述方法 只是简单的加法性线性探测 如果采用对数级别线程探测
  • AcWing 1250. 格子游戏 并查集模板题

    题 参考 并查集常用一维 所以对于坐标 x y 转换为x n y xy都要从0开始 其实就是3x3的转换为 0 1 2 3 4 5 6 7 8 这种 include
  • c++ STL map 结构体

    点击打开链接 点击打开链接 Map是STL的一个关联容器 它提供一对一 其中第一个可以称为关键字 每个关键字只能在map中出现一次 第二个可能称为该关键字的值 的数据处理能力 由于这个特性 它完成有可能在我们处理一对一数据的时候 在编程上提