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 ...
随机推荐
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
- HDU 1418 抱歉 (欧拉公式)
[题目链接]:pid=1418">click here~~ [题目大意]: 假设平面上有n个点,而且每一个点至少有2条曲线段和它相连,就是说,每条曲线都是封闭的.同一时候,我们规定: ...
- 系统安全-Firewall
Netfilter/iptables是与最新的2.6.x版本Linux内核集成的ip信息包过滤系统.如果Linux系统连接到因特网或LAN.服务器或连接LAN和因特网的代理服务器,则该系统有理由在Li ...
- 你必须了解的java内存管理机制(一)-运行时数据区
前言 本打算花一篇文章来聊聊JVM内存管理机制,结果发现越扯越多,于是分了四遍文章(文章讲解JVM以Hotspot虚拟机为例,jdk版本为1.8),本文为其中第一篇.from 你必须了解的java内存 ...
- 在Linux的Eclipse下搭建Android环境
http://blog.csdn.net/lyonte/article/details/6407242 一.Java环境安装配置详见<在Linux下搭建Java环境>http://blog ...
- UITableView基础入门
新建一个Single View Application 添加一个空类如下: using System; using UIKit; using Foundation; namespace BasicTa ...
- Python 008- 游戏2048
#-*- coding:utf-8 -*- import curses from random import randrange, choice # generate and place new ti ...
- cocos2d-js v3新特性
1.游戏对象 使用cc.game单例代替了原有的cc.Application以及cc.AppControl 2.属性风格API 旧的API ...
- socket基本使用
UDP发送和接收 MainRecv.cpp #include <iostream> #include <WinSock2.h> #include <sstream> ...
- int 转十六进制
//使用1字节就可以表示bpublic static String numToHex8(int b) { return String.format("%02x", b ...