C++ STL 学习笔记__(8)map和multimap容器
10.2.9 Map和multimap容器
map/multimap的简介
² map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对。它提供基于key的快速检索能力。
² map中key值是唯一的。集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。
² map的具体实现采用红黑树变体的平衡二叉树的数据结构。在插入操作和删除操作上比vector快。
² map可以直接存取key所对应的value,支持[]操作符,如map[key]=value。
² multimap与map的区别:map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符。
² #include <map>
map/multimap对象的默认构造
map/multimap采用模板类实现,对象的默认构造形式:
map<T1,T2> mapTT;
multimap<T1,T2> multimapTT;
如:
map<int, char> mapA;
map<string,float> mapB;
//其中T1,T2还可以用各种指针类型或自定义类型
map的插入与迭代器
² map.insert(...); //往容器插入元素,返回pair<iterator,bool>
² 在map中插入元素的三种方式:
假设 map<int, string> mapStu;
² 一、通过pair的方式插入对象
mapStu.insert( pair<int,string>(3,"小张") );
² 二、通过pair的方式插入对象
mapStu.inset(make_pair(-1, “校长-1”));
² 三、通过value_type的方式插入对象
mapStu.insert( map<int,string>::value_type(1,"小李") );
² 四、通过数组的方式插入值
mapStu[3] = “小刘";
mapStu[5] = “小王";
² 前三种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool>
² 第四种方法非常直观,但存在一个性能的问题。插入3时,先在mapStu中查找主键为3的项,若没发现,则将一个键为3,值为初始化值的对组插入到mapStu中,然后再将值修改成“小刘”。若发现已存在3这个键,则修改这个键对应的value。
² string strName = mapStu[2]; //取操作或插入操作
² 只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。
假设 map<int, string> mapA;
pair< map<int,string>::iterator, bool > pairResult = mapA.insert(pair<int,string>(3,"小张")); //插入方式一
int iFirstFirst = (pairResult.first)->first; //iFirst == 3;
string strFirstSecond = (pairResult.first)->second; //strFirstSecond为"小张"
bool bSecond = pairResult.second; //bSecond == true;
mapA.insert(map<int,string>::value_type(1,"小李")); //插入方式二
mapA[3] = "小刘"; //修改value
mapA[5] = "小王"; //插入方式三
string str1 = mapA[2]; //执行插入 string() 操作,返回的str1的字符串内容为空。
string str2 = mapA[3]; //取得value,str2为"小刘"
//迭代器遍历
for (map<int,string>::iterator it=mapA.begin(); it!=mapA.end(); ++it)
{
pair<int, string> pr = *it;
int iKey = pr.first;
string strValue = pr.second;
}
map.rbegin()与map.rend() 略。
² map<T1,T2,less<T1> > mapA; //该容器是按键的升序方式排列元素。未指定函数对象,默认采用less<T1>函数对象。
² map<T1,T2,greater<T1>> mapB; //该容器是按键的降序方式排列元素。
² less<T1>与greater<T1> 可以替换成其它的函数对象functor。
² 可编写自定义函数对象以进行自定义类型的比较,使用方法与set构造时所用的函数对象一样。
² map.begin(); //返回容器中第一个数据的迭代器。
² map.end(); //返回容器中最后一个数据之后的迭代器。
² map.rbegin(); //返回容器中倒数第一个元素的迭代器。
² map.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
map对象的拷贝构造与赋值
map(const map &mp); //拷贝构造函数
map& operator=(const map &mp); //重载等号操作符
map.swap(mp); //交换两个集合容器
例如:
map<int, string> mapA;
mapA.insert(pair<int,string>(3,"小张"));
mapA.insert(pair<int,string>(1,"小杨"));
mapA.insert(pair<int,string>(7,"小赵"));
mapA.insert(pair<int,string>(5,"小王"));
map<int ,string> mapB(mapA); //拷贝构造
map<int, string> mapC;
mapC = mapA; //赋值
mapC[3] = "老张";
mapC.swap(mapA); //交换
map的大小
² map.size(); //返回容器中元素的数目
² map.empty();//判断容器是否为空
map<int, string> mapA;
mapA.insert(pair<int,string>(3,"小张"));
mapA.insert(pair<int,string>(1,"小杨"));
mapA.insert(pair<int,string>(7,"小赵"));
mapA.insert(pair<int,string>(5,"小王"));
if (mapA.empty())
{
int iSize = mapA.size(); //iSize == 4
}
map的删除
² map.clear(); //删除所有元素
² map.erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
² map.erase(beg,end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
² map.erase(keyElem); //删除容器中key为keyElem的对组。
map<int, string> mapA;
mapA.insert(pair<int,string>(3,"小张"));
mapA.insert(pair<int,string>(1,"小杨"));
mapA.insert(pair<int,string>(7,"小赵"));
mapA.insert(pair<int,string>(5,"小王"));
//删除区间内的元素
map<int,string>::iterator itBegin=mapA.begin();
++ itBegin;
++ itBegin;
map<int,string>::iterator itEnd=mapA.end();
mapA.erase(itBegin,itEnd); //此时容器mapA包含按顺序的{1,"小杨"}{3,"小张"}两个元素。
mapA.insert(pair<int,string>(7,"小赵"));
mapA.insert(pair<int,string>(5,"小王"));
//删除容器中第一个元素
mapA.erase(mapA.begin()); //此时容器mapA包含了按顺序的{3,"小张"}{5,"小王"}{7,"小赵"}三个元素
//删除容器中key为5的元素
mapA.erase(5);
//删除mapA的所有元素
mapA.clear(); //容器为空
map的查找
² map.find(key); 查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
² map.count(keyElem); //返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
|
map<int,string>::iterator it=mapStu.find(3); if(it == mapStu.end()) { //没找到 } else { //找到了 pair<int, string> pairStu = *it; int iID = pairStu.first; //或 int iID = it->first; string strName = pairStu.second; //或 string strName = it->second; } |
|
² map.lower_bound(keyElem); //返回第一个key>=keyElem元素的迭代器。 ² map.upper_bound(keyElem); // 返回第一个key>keyElem元素的迭代器。 例如: mapStu是用map<int,string>声明的容器,已包含{1,"小李"}{3,"小张"}{5,"小王"}{7,"小赵"}{9,"小陈"}元素。map<int,string>::iterator it; it = mapStu.lower_bound(5); //it->first==5 it->second=="小王" it = mapStu.upper_bound(5); //it->first==7 it->second=="小赵" it = mapStu.lower_bound(6); //it->first==7 it->second=="小赵" it = mapStu.upper_bound(6); //it->first==7 it->second=="小赵" |
|
² map.equal_range(keyElem); //返回容器中key与keyElem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。 以上函数返回两个迭代器,而这两个迭代器被封装在pair中。 例如 map<int,string> mapStu; ... //往mapStu容器插入元素{1,"小李"}{3,"小张"}{5,"小王"}{7,"小赵"}{9,"小陈"} pair< map<int,string>::iterator , map<int,string>::iterator > pairIt = mapStu.equal_range(5); map<int, string>::iterator itBeg = pairIt.first; map<int, string>::iterator itEnd = pairIt.second; //此时 itBeg->first==5 , itEnd->first == 7, itBeg->second=="小王", itEnd->second=="小赵" |
|
Multimap 案例: //1个key值可以对应多个valude =è分组 //公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) //人员信息有:姓名,年龄,电话、工资等组成 //通过 multimap进行 信息的插入、保存、显示 //分部门显示员工信息 |
#include<iostream>
#include <string>
#include "map"
using namespace std; //map元素的添加/遍历/删除基本操作
void main1101()
{
map<int,string> map1; //方法1
map1.insert(pair<int,string>(,"teacher01"));
map1.insert(pair<int,string>(,"teacher02")); //方法2
map1.insert(make_pair(,"teacher04"));
map1.insert(make_pair(,"teacher05")); //方法3
map1.insert(map<int,string>::value_type(,"teacher06"));
map1.insert(map<int,string>::value_type(,"teacher07")); //方法4
map1[] = "teacher08";
map1[] = "teacher09"; //容器的遍历
for (map<int,string>::iterator it = map1.begin();it != map1.end();it++)
{
cout<<it->first<<"\t"<<it->second<<endl;
}
cout<<"遍历结束"<<endl; while (!map1.empty())
{
map<int,string>::iterator it = map1.begin();
cout<<it->first<<"\t"<<it->second<<endl;
map1.erase(it);
} } //四种插入方法的异同
//前三种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool> 若key已经存在,报错
//方法四:若key已经存在则修改
void main1102()
{
map<int,string> map1; //typedef pair<iterator, bool> _Pairib; //方法1
pair<map<int,string>::iterator, bool> mypair1 = map1.insert(pair<int,string>(,"teacher01"));
map1.insert(pair<int,string>(,"teacher02")); //方法2
pair<map<int,string>::iterator, bool> mypair3 = map1.insert(make_pair(,"teacher04"));
map1.insert(make_pair(,"teacher05")); //方法3
pair<map<int,string>::iterator, bool> mypair5 = map1.insert(map<int,string>::value_type(,"teacher06")); if (mypair5.second != true)
{
cout<<"插入失败"<<endl;
}
else
{
cout<<mypair5.first->first<<"\t"<<mypair5.first->second<<endl;
} pair<map<int,string>::iterator, bool> mypair6 = map1.insert(map<int,string>::value_type(,"teacher07")); if (mypair6.second != true)
{
cout<<"插入失败"<<endl;
}
else
{
cout<<mypair6.first->first<<"\t"<<mypair6.first->second<<endl;
} //方法4
map1[] = "teacher07";
map1[] = "teacher77";// //容器的遍历
for (map<int,string>::iterator it = map1.begin();it != map1.end();it++)
{
cout<<it->first<<"\t"<<it->second<<endl;
}
cout<<"遍历结束"<<endl; while (!map1.empty())
{
map<int,string>::iterator it = map1.begin();
cout<<it->first<<"\t"<<it->second<<endl;
map1.erase(it);
}
} void main()
{
//main1101();
main1102();
cout<<"hello..."<<endl;
return;
}
#include<iostream>
using namespace std;
#include "map"
#include "string" //Multimap 案例: //1个key值可以对应多个valude =è分组 //公司有销售部 sale (员工2名)、技术研发部 development (1人)、财务部 Financial (2人) //人员信息有:姓名,年龄,电话、工资等组成 //通过 multimap进行 信息的插入、保存、显示 //分部门显示员工信息 //
class Person
{
public:
string name;
int age;
string tel;
double sal;
};
void main1201()
{
Person p1,p2,p3,p4,p5; p1.name = "王1";
p1.age = ; p2.name = "王2";
p2.age = ; p3.name = "王3";
p3.age = ; p4.name = "王4";
p4.age = ; p5.name = "王5";
p5.age = ; multimap<string,Person> map2;
//sal部门
map2.insert(make_pair("sale",p1));
map2.insert(make_pair("sale",p2)); //development 部门
map2.insert(make_pair("development",p3));
map2.insert(make_pair("development",p4)); //Financial部门
map2.insert(make_pair("Financial",p5)); for (multimap<string,Person>::iterator it = map2.begin();it != map2.end();it++)
{
cout<<it->first<<"\t"<<it->second.name<<endl;
} cout<<"遍历结束"<<endl; //
int num2 = map2.count("development");
cout<<"development部门人数="<<map2.count("development"); cout<<"development部门员工信息"<<endl;
multimap<string,Person>::iterator it2 = map2.find("development");
int tag = ;
while (it2 != map2.end() && tag<num2)
{
cout<<it2->first<<"\t"<<it2->second.name<<endl;
it2++;
}
} //age = 32 修改成name32
void main1202()
{
Person p1,p2,p3,p4,p5; p1.name = "王1";
p1.age = ; p2.name = "王2";
p2.age = ; p3.name = "王3";
p3.age = ; p4.name = "王4";
p4.age = ; p5.name = "王5";
p5.age = ; multimap<string,Person> map2;
//sal部门
map2.insert(make_pair("sale",p1));
map2.insert(make_pair("sale",p2)); //development 部门
map2.insert(make_pair("development",p3));
map2.insert(make_pair("development",p4)); //Financial部门
map2.insert(make_pair("Financial",p5)); cout<<"按照条件 检索数据 进行修改"<<endl;
for (multimap<string,Person>::iterator it = map2.begin();it != map2.end();it++)
{
//cout<<it->first<<"\t"<<it->second.name<<endl;
if (it->second.age == )
{
it->second.name = "name32";
}
} for (multimap<string,Person>::iterator it = map2.begin();it != map2.end();it++)
{
cout<<it->first<<"\t"<<it->second.name<<endl;
} cout<<"遍历结束"<<endl; }
void main()
{
main1201();
main1202();
cout<<"hello..."<<endl;
return;
}
资料来源:传智播客 仅供学习研究
C++ STL 学习笔记__(8)map和multimap容器的更多相关文章
- STL学习系列九:Map和multimap容器
1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- C++ STL 学习笔记__(7)Set和multiset容器
10.2.8 Set和multiset容器 set/multiset的简介 ² set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指 ...
- C++ STL 学习笔记__(6)优先级队列priority_queue基本操作
10.2.7优先级队列priority_queue v 最大值优先级队列.最小值优先级队列 v 优先级队列适配器 STL priority_queue v 用来开发一些特殊的应用,请对stl的类 ...
- C++ STL 学习笔记__(5)list
10.2.6List容器 List简介 ² list是一个双向链表容器,可高效地进行插入删除元素. ² list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.It++(ok) i ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Go语言学习笔记十三: Map集合
Go语言学习笔记十三: Map集合 Map在每种语言中基本都有,Java中是属于集合类Map,其包括HashMap, TreeMap等.而Python语言直接就属于一种类型,写法上比Java还简单. ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
随机推荐
- 关于kernel-devel、kernel-header和kernel src的区别
A kernel-header package would contain 'header files' needed by some applications which would be buil ...
- pong game using ncurses
bounce2d2.c /* * bounce2d 1.0 * bounce a character (default is 'o') around the screen * defined by s ...
- SQL Server Management Studio记住的密码丢失的问题
不知道各位经常使用SSMS的时候有没有碰到过这样的烦恼: 记住的密码总是丢失: 步骤如下: 登陆时,选择记住密码 在任何一个存储过程上点击右键,选择修改 这时候再次连接对象资源管理器时,刚刚记住的密码 ...
- 异常System.BadImageFormatException
[问题描述] Server Error in '/' Application. Could not load file or assembly 'WebDemo' or one of its depe ...
- java实现安全证书相关操作
https://blog.csdn.net/zhushanzhi/article/details/77864516 版权声明:本文为博主原创文章,未经博主允许不得转载. package test; i ...
- 56_实现类似spring的可配置的AOP框架
> config.properties 配置文件 key=类名 > BeanFactory Bean工厂,负责得到bean getBean("xxx") &g ...
- pt-table-checksum工具MySQL主从复制数据一致性
所使用的工具是pt-table-checksum 原理是: 在主上执行检查语句去检查 mysql主从复制的一致性,生成 replace 语句,然后通过复制传递到从库,再通过update 更新 mast ...
- Angular2学习笔记(1)——Hello World
1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之前主要使用的是jQuery,由于 ...
- Nodejs学习资源汇总
Node.js v6.3.1 Documentation https://nodejs.org/dist/latest-v6.x/docs/api/ npm官网 https://www.npmjs ...
- Swift: Associated Types--为什么协议使用关联类型而不是泛型
关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...