c++ STL map 结构体
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));
}
- /******************************************************************
- map的基本操作函数:
- C++ Maps是一种关联式容器,包含“关键字/值”对
- begin() 返回指向map头部的迭代器
- clear() 删除所有元素
- count() 返回指定元素出现的次数
- empty() 如果map为空则返回true
- end() 返回指向map末尾的迭代器
- equal_range() 返回特殊条目的迭代器对
- erase() 删除一个元素
- find() 查找一个元素
- get_allocator() 返回map的配置器
- insert() 插入元素
- key_comp() 返回比较元素key的函数
- lower_bound() 返回键值>=给定元素的第一个位置
- max_size() 返回可以容纳的最大元素个数
- rbegin() 返回一个指向map尾部的逆向迭代器
- rend() 返回一个指向map头部的逆向迭代器
- size() 返回map中元素的个数
- swap() 交换两个map
- upper_bound() 返回键值>给定元素的第一个位置
- value_comp() 返回比较元素value的函数
- ====================================================================
- 1、map构造
- map<int, string> mapStudent;
- 2、map添加数据
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(map<int, string>::value_type(2, "student_two"));
- mapStudent[3] = "student_three";
- ********************************************************************/
- #pragma warning (disable:4786)
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- cout<<"三种插入方式:"<<endl;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(map<int, string>::value_type(2, "student_two"));
- mapStudent[3] = "student_three";
- mapStudent.insert(map<int, string>::value_type(4, "student_four"));
- pair<map<int,string>::iterator,bool> InsertPair; //判断是否插入成功
- InsertPair = mapStudent.insert(map<int,string>::value_type(5,"student_five"));
- if(InsertPair.second == true)
- {
- //cout<<InsertPair.first.operator++<<endl; //求解??不知道怎么应用第一个数据
- }
- cout<<"三种遍历方式:"<<endl;
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- map<int, string>::reverse_iterator iters;
- for(iters = mapStudent.rbegin(); iters != mapStudent.rend(); iters++)
- {
- cout<<iters->first<<" "<<iters->second<<endl;
- } //逆序输出
- cout<<"数组的输出形式:"<<endl;
- for(int iIndex=0;iIndex < mapStudent.size();iIndex++) //size()返回成员的个数
- {
- cout<<mapStudent[iIndex]<<endl;
- }
- cout<<mapStudent.count(1)<<endl; //count()判断关键字是否存在,返回1表示存在,0
- iter = mapStudent.find(1); //find()关键字存在时,返回数据所在位置的迭代器,否则返回end()返回的迭代器
- if( iter != mapStudent.end() )
- {
- cout<<"数据存在:"<<iter->first<<" "<<iter->second<<endl;
- mapStudent.erase(iter); //用迭代删除数据
- }
- else
- {
- cout<<"数据不存在!"<<endl;
- }
- int n = mapStudent.erase(3); //用关键字删除,如果删除了会返回1,否则返回0
- iter = mapStudent.lower_bound(2); //返回2的迭代器
- cout<<iter->second<<endl;
- iter = mapStudent.upper_bound(2); //返回3的迭代器
- cout<<iter->second<<endl;
- /*Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字*/
- pair<map<int,string>::iterator,map<int,string>::iterator> MapPair;
- MapPair = mapStudent.equal_range(2);
- if( MapPair.first == MapPair.second )
- {
- cout<<"Do not find"<<endl;
- }
- else
- {
- cout<<"Find"<<endl;
- }
- //删除一个前闭后开的集合,这是STL的特性
- mapStudent.earse(mapStudent.begin(), mapStudent.end());
- }
c++ STL map 结构体的更多相关文章
- map 结构体
map<node,int> 需要运算符重载< 请注意,不同的node,请务必让它们可以区分出来(node a,b a<b or b<a) 如 node { int a,i ...
- L2-002. 链表去重(map结构体,精彩的代码)
链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值 ...
- stl实现结构体排序关键语法要点(sort)
sort函数,调用时使用函数头: #include <algorithm> sort(begin,end);用来表示一个范围. int _tmain(int argc, _TCHAR* a ...
- STL map、set中key为结构体的用法
下面是map定义的结构: // TEMPLATE CLASS map template<class _Kty, class _Ty, class _Pr = less<_Kty>, ...
- CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
User-Defined Data Types in Messages(用户自定义类型)All user-defined types must be explicitly “announced” so ...
- map中结构体做关键字的注意事项
序: 今天做一道题,由于递归函数比较恶心,如果用记忆化搜索,数据范围极大却又用不全(二维数组存的话直接炸).所以决定干脆使用stl::map存储(反正有O2优化),但是执行insert的时候,编译器却 ...
- 结构体作为map的key或放入set中,需要重载<运算符
结构体作为map的key或放入set中,需要重载<运算符,如下: typedef struct tagRoadKey{ int m_i32Type; int m_i32Scale; ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
- 用set、map等存储自定义结构体时容器内部判别各元素是否相同的注意事项
STL作为通用模板极大地方便了C++使用者的编程,因为它可以存储任意数据类型的元素 如果我们想用set与map来存储自定义结构体时,如下 struct pp { double xx; double y ...
随机推荐
- 基于SpringSecurity和JWT的用户访问认证和授权
发布时间:2018-12-03 技术:springsecurity+jwt+java+jpa+mysql+mysql workBench 概述 基于SpringSecurity和JWT的用户访 ...
- 【linux环境】Linux环境 php连接oracle11g数据库(相关插件已备份至U盘)
1.环境:centos6 . LNMP(linux环境都可以,跟服务器没啥大关系) 2.前期准备:弄清楚 项目php的运行目录,php.ini的配置目录,php-config的运行目录 3.安装先知: ...
- ASP.NET MVC3-第02节-添加一个Controller (C#)
前言 ---------------------------- 第01节[翻译]01-ASP.NET MVC 3介绍 ---------------------------- MVC是“model-v ...
- Java多线程编程:Callable、Future和FutureTask浅析
通过前面几篇的学习,我们知道创建线程的方式有两种,一种是实现Runnable接口,另一种是继承Thread,但是这两种方式都有个缺点,那就是在任务执行完成之后无法获取返回结果,那如果我们想要获取返回结 ...
- 树莓派进阶之路 (024) - windows远程桌面连接树莓派通过xrdp服务(转)
本文转载:http://www.cnblogs.com/edgexie/p/6527992.html 在网上看到很多关于windows远程桌面连接树莓派的教程.我也按照教程试过了,遇到了几个坑.特意记 ...
- PowerDesigner使用:[3]创建索引
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- myeclipse 遇到的一些问题及解决方案
1..提示键配置 一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多 ...
- [转]gdb调试多进程和多线程命令
1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...
- js 万恶之源 是否滚动到底部?
let scrollHandle = (el) => { // 如果已经滚到底部了 if (el.scrollHeight - el.scrollTop === el.clientHeight) ...
- php分享十七:http状态码
一:http状态码 (200,301,302,304,305,400,401,403,404,500,501,502,503,504) HTTP状态码(HTTP Status Code)是用以表示网页 ...