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 ...
随机推荐
- 【Mood-14】龙虎榜 活跃在github中的1000位中国开发者
Last cache created on 2015-01-07 by Github API v3. ♥ made by hzlzh just for fun. Rank Gravatar usern ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:5.技术简介之Hibernate
目录 序言 配置 hibernate.cfg.xml配置文件 加载hibernate.cfg.html配置文件并获取Session 对象的注解配置 增删改查 具体的增删改查代码 数据库操作的封装 连接 ...
- java之打印机服务通俗做法
javax.print包是API的主包,其中包含的类和接口能够让你:1)发现打印服务(Print Services)2)指定打印数据的格式 3)从一个打印服务创建打印工作(print jobs) 4) ...
- 黑幕背后的Autorelease
http://blog.sunnyxx.com/2014/10/15/behind-autorelease/ 我是前言 Autorelease机制是iOS开发者管理对象内存的好伙伴,MRC中,调用[o ...
- 深入理解计算机系统_3e 第九章家庭作业 CS:APP3e chapter 9 homework
9.11 A. 00001001 111100 B. +----------------------------+ | Parameter Value | +--------------------- ...
- python_73_pickle序列化(接72)
# json(为字符串形式)用于不同语言之间的数据交互,只适用于简单的数据交互,字典之类可以,函数就不行了,如下例 ''' import json def say(name):print('Hi!', ...
- DongDong坐飞机
题目连接:https://ac.nowcoder.com/acm/contest/904/D 第一次研究了一下这种题型,还是比较好理解的,因为有半价次数的限制,所以要把每一中情况都写出来,dp[现在的 ...
- 题解 CF734A 【Anton and Danik】
本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...
- Return-to-dl-resolve浅析
本文介绍一种CTF中的高级rop技巧-Return-to-dl-resolve,不久前的0CTF中的babystack和blackhole就用到了这个技巧. 预备知识 在开始本文前希望大家能预先了解一 ...
- 软件杯python-flask遇到的坑有感!
大三下,对于我考研的人来说,时间不要太紧张,参加软件杯也是系主任要求,题目是公共地点人流量的检测,个人还是个菜鸟,但是把遇到的一些大家可能不小心会出现的问题贴出来,困扰我很久,还没睡好觉!!! Que ...