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 ...
随机推荐
- Shape详解
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- 部署JavaWeb时出现 If a file is locked,you can wait until
在部署JavaWeb程序时出现了if a file is locked ,you can wait until the lock stop的问题,这个主要是classpath目录出错或者jar包未导入 ...
- 【BZOJ4033】[HAOI2015] 树上染色(树形DP)
点此看题面 大致题意: 给你一棵点数为N的带权树,要你在这棵树中选择K个点染成黑色,并将其他的N-K个点染成白色.要求你求出黑点两两之间的距离加上白点两两之间距离的和的最大值. 树形\(DP\) 这道 ...
- 【CF799B】T-shirt buying(一道很水的小根堆)
点此看题面 大致题意: 有\(n\)件T恤衫,告诉你每件T恤衫的价格以及它正面和反面的颜色(\(1≤\)颜色的编号\(≤3\)),现在有m个顾客,已知每个人想要的衣服的颜色(一件T恤衫只要有一面的颜色 ...
- 使用RichTextBox控件保存文件
实习效果: 知识运用: RichTextBox控件的SaveFile方法 SaveFileDialog对象的ShowDialog方法 实现代码: private void 打开RTF文件ToolStr ...
- MRCA|Wright–Fisher population genetic model|SNP rate
(Panda has a high heterozygosity rate) 通过对mtDNA(为了预测SNP的密度)的分析,可知panda的多样性,当前全基因组数据才能完全建立模型. mitocho ...
- Bootstrap 警告框(Alert)插件
警告消息大多来是用来向终端用户提示警告或确认的消息,使用警告框插件,您可以向所有的警告框消息添加取消功能. 用法 您有以下两种方式启用警告框的可取消功能. 1.通过data属性:通过数据添加可取消功能 ...
- 2018.10.30 NOIp模拟赛T2 数字对
[题目描述] 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为 n 的序列{ai},她想找出一段区间[L, R](1 <= L <= ...
- NOIP2018 全国热身赛 第二场 (不开放)
NOIP2018 全国热身赛 第二场 (不开放) 题目链接:http://noi.ac/contest/26/problem/60 一道蛮有趣的题目. 然后比赛傻逼了. 即将做出来的时候去做别的题了. ...
- Java - BigDecimal四舍五入注意事项
如上图,精度后只有一位时,是五舍六入. 如上图,精度后只有第一位不为0时,也是五舍六入. 如上图,精度后至少有两位不为0时,才是四舍五入.