c++中stl----map
1 map的本质
(1)关联式容器,键值对应
(2)增加和删除节点对迭代器的影响很小。
(3)对于迭代器来说不可以修改键值,只能修改对应的实值。
(4)map内部数据的祖居是自建一颗红黑树(或者说是平衡二叉树),具有自动排序的功能。
2 map的查增删
(1)map的插入
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
//方式一 pair
map<int,string> mapStudent;
mapStudent.insert(pair<int,string>(1,"lan"));
mapStudent.insert(pair<int,string>(2,"ji"));
mapStudent.insert(pair<int,string>(1,"kjh"));
map<int,string>::iterator iter;
map<int,string>::iterator iter1;
//方式二
map<int string> mapStudent1;
mapStudent1.insert(map<int,string>::value_type(1,"nihao"));
mapStudent1.insert(map<int,string>::value_type(1,"ben"));
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}
2 map的遍历
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.find(1);
if(iter!=mapStudent.end())
{
cout<<"找到了 value="<<iter->second<<endl;
}else
{
cout<<"没有找到"<<endl;
}
return 0;
}
3 map的删除
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.begin();
for(;iter!=mapStudent.end();)
{
if((*iter).sencond=="stu_one")
{
mapStudent.erase(iter++);//iter被erase以后就会失效 所以后面不能再有iter++
}else
{
++iter;
}
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}
4 map排序
默认按照key从小到大。从大到小greater 相反less
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string,int,greater<string>> mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
自定义排序方式
#include <string>
#include <iostream>
using namespace std;
struct CmpByKeyLength
{
bool operator()(const string &k1,const string&k2){
return k1.length()<k2.length();
}
};
int main()
{
map<string,int,CmpByKeyLength>mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
------>加油美好的一天
c++中stl----map的更多相关文章
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...
- STL中关于map和set的四个问题?
STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- STL中的map和unordered_map
STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...
- STL中的map和hash_map
以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
随机推荐
- Linux下查看系统CPU个数、核心数、线程数
1.查看物理CPU个数 (env) root@vmware01:~# grep 'physical id' /proc/cpuinfo physical physical physical physi ...
- 关于Yapi出现 请求异常,请检查 chrome network 错误信息...
项目开发中由于后台接口还没有,打算使用mock模拟本地数据,配置好接口,运行接口出现 检查了cross-request插件是否安装以及激活,发现没有问题,最后发现是我的请求地址写错了,,这里请求地址需 ...
- LeetCode(82)题解: Remove Duplicates from Sorted List II
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, ...
- Topcoder SRM 638 DIV 2 (大力出奇迹)
水题,就是一个暴力.大力出奇迹. Problem Statement There is a narrow passage. Inside the passage there are some wo ...
- 【BZOJ3252】攻略 DFS序+线段树(模拟费用流)
[BZOJ3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛> ...
- SAM4E单片机之旅——7、LED闪烁之TC中断
RTT主要用做一个全局的定时器,而且不太通用.现在尝试使用一个更为通用的定时器进行定时:定时计数器(Timer Counter, TC). TC提供了广泛的功能,主要可以分为对输入的测量,以及波形的输 ...
- Post Man 调用CRMAPI
官方文档 https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/setup-postman ...
- iOS 第三方登录之 QQ登录
一. 首先需要下载腾讯qq登录所需的库,下载地址是http://open.qq.com/ . 需要用到的有TencentOpenAPI.framework 和TencentOpenApi_IOS_Bu ...
- 7-12 畅通工程之最低成本建设问题(30 point(s)) 【PRIME】
7-12 畅通工程之最低成本建设问题(30 point(s)) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间 ...
- jquery特效(4)—轮播图②(定时自动轮播)
周末出去逛完街,就回公司好好地研究代码了,也算是把定时自动轮播程序写出来了,特意说明一下,这次的轮播图是在昨天随笔中jquery特效(3)—轮播图①(手动点击轮播)的基础上写出来的,也就是本次随笔展示 ...