STL学习笔记5--map and multimap
Maps是一种关联式容器,包含“关键字/值”对。 Multimaps和maps很相似,但是MultiMaps允许重复的元素。
简单介绍:
1、声明,首先包含头文件 “map”
map <int,string> test1,test2;//
map <int,string>::iterator it1,it2;//迭代器
multimap <int,string> test3;
multimap <int,string>::iterator it3;
2、插入数据,可使用三种方法:
第一种,使用pair函数
test1.insert(pair<int,string>(,"song"));
test1.insert(pair<int,string>(,"zhang"));
test1.insert(pair<int,string>(,"wang"));
第二种,使用value_type类型
test1.insert(map<int,string>::value_type(,"qian"));
test1.insert(map<int,string>::value_type(,"sun"));
第三种,使用数组方式,,可以覆盖原来数据,前两中不能改变数据,如果存在则插入失败
test1[] = "mao";
test1[] = "guang";
前两种情况,插入失败后可以通过以下方法检查
//测试是否插入成功
pair<map<int,string>::iterator,bool> insert_Pair;
insert_Pair = test1.insert(pair<int,string>(,"Replace"));
if (insert_Pair.second == true)
{
cout<<"insert successfully"<<endl;
}
else
{
cout<<"insert failure"<<endl;
}
3、遍历数据,可使用以下几种方法
第一,正向遍历
for (it1 = test1.begin();it1 != test1.end();it1++)
{
cout<<it1->first<<"-----" << it1->second<<endl;
}
第二,逆向遍历,使用反向迭代器
cout<<"反向迭代器"<<endl;
//rbegin()指向最后一个元素,rend()指向第一个元素前面,这里++是指往前走一个位置
map<int,string>::reverse_iterator reverseIte;
for (reverseIte = test1.rbegin();reverseIte != test1.rend();reverseIte++)
{
cout<<reverseIte->first<<"-----" << reverseIte->second<<endl;
}
第三,使用数组进行遍历
//使用数组方式进行遍历
for (int i = ;i <= test1.size();i++)
{
cout<<i<<"-----"<<test1[i]<<endl;
}
4、查找和判断
第一,使用count进行判断
//count,判断
int i=;
for (it1 = test1.begin();it1 != test1.end();i++,it1++)
{
cout<<i;
if (test1.count(i)>)//元素存在
{
cout<<"is a element of map"<<endl;
}
else
{
cout<<"is not a element of map"<<endl;
}
}
第二,使用find判断
it2 = test1.find();//查找
if (it2 != test1.end())
{
cout<<"find it:"<<it2->first<<"---"<<it2->second<<endl;
}
else
{
cout<<"not find it"<<endl;
}
5、删除元素
//删除元素
it2 = test1.find();
test1.erase(it2);
这些操作基本上都和set的差不多,好多函数一模一样。
STL学习笔记5--map and multimap的更多相关文章
- STL学习笔记— —容器map和multimap
简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- 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 ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
随机推荐
- 爱加密so保护简单脱壳测试
1. 最近研究so文件的保护,在网上搜索发现爱加密支持对so文件的保护,然后联系客户,本来是想让客户保护一个自己的so文件来做测试的,结果客户各种不愿意,说要签什么XX协议后才能给so保护,各种蛋 ...
- Notification高级技巧
观察Notification这个类,你会发现里面还有很多我们没有使用过的属性.先来看看sound这个属性吧,它可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.sound 这个属性 ...
- Android商城开发系列(十一)—— 首页秒杀布局实现
首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...
- Dll注入:x86/X64 SetThreadContext 注入
在<Windows核心编程>第七章说到了线程优先级等知识,其中谈到了ThreadContext线程上下背景文. 其中介绍了GetThreadContext函数来查看线程内核对象的内部,并获 ...
- 【51nod1743】雪之国度(最小生成树+倍增)
点此看题面 大致题意: 给你一张无向连通图,其中每条边的边权为这条边连接的两点的权值之差.每次询问两点之间是否存在两条不重复的路径,若存在则输出这两条路径上最大值的最小值. 大致思路 这题显然就是要让 ...
- python_23_tuple
#元组只能统计和获取下表,不能插入之类的.元组和列表差不多,也是存一组数,只是它一旦创建,便不能再修改,所以又叫只读列表 names=('QiZhiguang','LiuGuannan','Liang ...
- window下安装ubuntu(ubuntu可删除)
进入ububtu13.04的安装界面,这里我们选择了“中文(简体)”,然后单击安装: 下图是现场拍的: 出现如下图时,请根据需要选择,然后单击“继续” , 接下来会出现问你是否要连接网络,我们选择 ...
- spring xml 配置文件中标签的解析
一个springmvc配置文件的例子为: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- IPython安装过程 @win7 64bit
http://www.360doc.com/content/14/0902/11/16740871_406476389.shtml 为了测验测验一下IPython的应用,今天折腾了好久的从安装包msi ...
- html5shiv.js的作用是
解析 html5shiv主要解决HTML5提出的新的元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式.让CSS 样式应用在未知元素上只需执行 document.cre ...