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 ...
随机推荐
- 网络编程——基于UDP的网络化CPU性能检测
网络化计算机性能检测软件的开发,可对指定目标主机的CPU利用率进行远程检测,并自动对远程主机执行性能指标进行周期性检测,最终实现图形化显示检测结果. 网络通信模块:(客户端类似,因为udp是对等通信) ...
- RAC基本使用
@interface ViewController () @property (weak, nonatomic) IBOutlet lwRedView *redView; @property (wea ...
- flash + php对称密钥加密的交互
这几天研究了下php和flash中的对称密钥加密的交互问题,经过研究以后决定,在项目中使用aes加密.问题也就来了,在flash中的加密数据如何与php的amf进行数据交互,最终决定使用base64编 ...
- 多线程中使用HttpContext.Current为null的解决办法
HttpContext.Current.Server.MapPath(logFile) 这个是得到具体路径的方法 正常情况下是可以的 多线程情况下就为null 下边的代码原本的作用是把网站的异常 ...
- java web用户登录界面
做这次实验,主要用到了mysql java web 的 内容 实验代码: IUserDao.java package com.jaovo.msg.dao; import java.util.List ...
- Oracle分页抽数存储过程
--outTotal是需要返回的总数,v_loginUserId是传入的登录人ID,抽取他的客户,v_CurrPage是传入的第几页,v_pageSize传入的每页数据条数. ) FROM tb_cu ...
- 2.3.3 zerosum 和为零
#include<bits/stdc++.h> using namespace std; ],a; ]={' ','+','-'}; void out() { ;i<a;i++) c ...
- python笔记-dict字典的方法
#!/usr/bin/env python #-*- coding:utf-8 -*- #打印0001-9999的数字 for i in range(9999): s = "%04d&quo ...
- 与SVN相关的程序的调试问题【转】
解决eclipse中出现Resource is out of sync with the file system问题. 分析:有时候因为时间紧迫的原因,所以就没去管它,今天再次遇到它,实在看着不爽,所 ...
- 【CSS】简略说明css的权重之分
/*权重 :id > class > 标签 (小环境) 权重:内联 > 内部 > 外部 (大环境) 小环境处于内部环境中 */ <style> #p1{ /* id ...