C++ - STL - map的基础操作
STL - map常用方法
map简述
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,其作用类似于python之中的字典,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
map的基本操作函数:
优点
1.自动建立Key - value的对应。key 和 value可以是任意你需要的类型
2.根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次
3.快速插入Key -Value 记录
4.快速删除记录
5.根据Key 修改value记录
1.创建map容器
首先需要引入map的头文件
#include <map>
使用模板map<键,值> name(给map容器的命名)
下面列出常用的几种
map<int,int>,map<string,int>,map<int,string>
map<char,int>,map<int,char>.
插入数据
- map提供了三种插入方法
1.用insert函数插入pair数据
2.用insert函数插入value_type数据
3.用数组方式插入数据
//使用pair插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
mapnum.insert(pair<int, string>(1, "one"));
mapnum.insert(pair<int, string>(2, "two"));
mapnum.insert(pair<int, string>(3, "three"));
//声名迭代器
map<int, string>::iterator iter;
for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
cout << iter->first << " " << iter->second << endl;
system("pause");
return 0;
}
//使用value_type插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
mapnum.insert(map<int, string>::value_type(1, "one"));
mapnum.insert(map<int, string>::value_type(2, "two"));
mapnum.insert(map<int, string>::value_type(3, "three"));
//声名迭代器
map<int, string>::iterator iter;
for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
cout << iter->first << " " << iter->second << endl;
system("pause");
return 0;
}
//使用数组的方式插入数据
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
mapnum[1] = "one";
mapnum[2] = "two";
mapnum[3] = "three";
//声名迭代器
map<int, string>::iterator iter;
for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
cout << iter->first << " " << iter->second << endl;
system("pause");
return 0;
}
遍历数据
- 向前迭代器和反向迭代器
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
map<int, string> mapnum;
int main()
{
mapnum.insert(pair<int, string>(1, "one"));
mapnum.insert(pair<int, string>(2, "two"));
mapnum.insert(pair<int, string>(3, "three"));
//声名迭代器
map<int, string>::iterator iter;
//反向迭代
map<int, string>::reverse_iterator iter1;
//第一种向前迭代器遍历
for (iter = mapnum.begin(); iter != mapnum.end(); iter++)
cout << iter->first << " " << iter->second << endl;
//第二种应用反向迭代器
for(iter1 = mapnum.rbegin();iter1 != mapnum.rend();iter1++)
cout << iter1->first << " " << iter1->second << endl;
int nsize = mapnum.size();
for (int i = 1; i <= nsize; i++)
{
cout << mapnum[i] << endl;
}
system("pause");
return 0;
}
**其他常用方法
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
map<int, string> m;
int main()
{
//使用数组方法插入数据
m[1] = "w";
m[2] = "x";
m[3] = "can";
m[4] = "do";
m[5] = "it";
m[4] = "haha";
//得到map的大小
int nsize = m.size();
//count()统计某指定元素出现的次数
cout << m.count(1) << endl;
//find()查找某一个元素
map<int, string>::iterator it;
it = m.find(1);
if (it == m.end())cout << "没有找到!" << endl;
else cout << "找到该元素!" << endl;
//erase 删除一个元素
it = m.find(1);
if (it == m.end())cout << "没有找到!" << endl;
else m.erase(1);
system("pause");
return 0;
}
C++ - STL - map的基础操作的更多相关文章
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- 对vector等STL标准容器的排序操作
[+] STL提供的Sort 算法 所有sort算法介绍 sort 中的比较函数 sort 的稳定性 全排序 局部排序 nth_element 指定元素排序 partition 和stable_par ...
- 对vector等STL标准容器进行排序操作(转!)
西方有句谚语:不要重复发明轮子! STL几乎封装了所有的数据结构中的算法,从链表到队列,从向量到堆栈,对hash到二叉树,从搜索到排序,从增加到删除......可以说,如果你理解了STL,你会发现你已 ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- numpy 基础操作
Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...
- List基础操作
/** * List基础操作 * Created by zhen on 2018/11/14. */ object ListDemo { def main(args: Array[String]) { ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
随机推荐
- springboot做api服务时不使用thymeleaf的相关警告DefaultTemplateResolverConfiguration 的消除
springboot在不配置thymeleaf相关属性的情况下,会提示如下的问题 DefaultTemplateResolverConfiguration - Cannot find template ...
- spark listener
最近在做一个需求,当spark程序在读数据或写数据时,将所读的条数或或所写的条数实时的展现出来,这里用到了SparkListener,sparklisten 可以获取spark 各个运行阶段的状态. ...
- centos7 安装hadoop2.7.6(分布式)
本文只做简单介绍,具体步骤操作请参考centos6.5 安装hadoop1.2.1亲测版 本篇只简单介绍安装步骤 1.安装目录 /usr/local/hadoop (HADOOP_HOME) 2,创建 ...
- 在myecplice中关联svn
1:下载插件 site-1.8.22 2:找到myecplic的安装目录 下的dropins 文件夹(例如:C:\Users\han\AppData\Local\MyEclipse Professio ...
- flume参数解析+启动参数解析
flume参数: #example.conf:单节点Flume配置 #命名此代理上的组件 a1.sources = r1 a1.sinks = k1 a1.channels = c1 #描述/配置源 ...
- 第十五节:Asp.Net Core中的各种过滤器(授权、资源、操作、结果、异常)
一. 简介 1. 说明 提到过滤器,通常是指请求处理管道中特定阶段之前或之后的代码,可以处理:授权.响应缓存(对请求管道进行短路,以便返回缓存的响应). 防盗链.本地化国际化等,过滤器用于横向处理业务 ...
- PowerBuilder学习笔记之14用户自定义对象
教程链接:https://wenku.baidu.com/view/9730d1c7aa00b52acec7ca05.html?re=view&rec_flag=default&sxt ...
- LOJ2392 JOISC2017 烟花棒 二分、贪心
传送门 先二分一个最大速度\(v\). 分析移动的性质.很显然的事情是在火焰两边的所有人都会往火焰的方向以最快的速度运动,这样可以使当前位置更早获得火焰,同时当前拥有火焰的若干个人为了传递火焰自然也会 ...
- vue实现跨域请求的设置
vue实现跨域请求,需要在vue.config.js里添加以下设置 proxy: { '/service/rest': { target: 'http://localhost:8080/autotab ...
- 封装:WPF绘制曲线视图
原文:封装:WPF绘制曲线视图 一.目的:绘制简单轻量级的曲线视图 二.实现: 1.动画加载曲线 2.点击图例显示隐藏对应曲线 3.绘制标准基准线 4.绘制蒙板显示标准区域 曲线图示例: 心电图示例: ...