STL——map
看到map这里,都不知道它主要是干嘛的,你有没有这样的疑问。
map的主要作用:提供对T类型的数据进行快速和高效的检索 。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。 注:map是自动按key值排序的,默认为升序或字典排序
(1)map的定义:
int nFindKey = ; //要查找的Key //定义一个条目变量(实际是指针) UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey); if(it == enumMap.end()) { cout<<"没找到"<<endl; } else { cout<<"找到了"<<endl; }
#include <map>
#include <iostream>
using namespace std;
int main( )
{
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter; m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) );
m1.insert ( pair <int, int> ( , ) ); m2.insert ( pair <int, int> ( , ) );
m2.insert ( pair <int, int> ( , ) ); m3.insert ( pair <int, int> ( , ) ); cout << "The original map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter->second;
cout << "." << endl;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map m1.swap( m2 );
cout << "After swapping with m2, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl; cout << "After swapping with m2, map m2 is:";
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl; // This is the specialized template version of swap
swap( m1, m3 );
cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
}
下面再说几个例子:
#include <iostream> #include <map> using namespace std; int main(void) { map<char,int,less<char> > map1; map<char,int,less<char> >::iterator mapIter; //char 是键的类型,int是值的类型 //下面是初始化,与数组类似 //也可以用map1.insert(map<char,int,less<char> >::value_type('c',3)); map1['c']=; map1['d']=; map1['a']=; map1['b']=; for(mapIter=map1.begin();mapIter!=map1.end();++mapIter) cout<<" "<<(*mapIter).first<<": "<<(*mapIter).second; //first对应定义中的char键,second对应定义中的int值 //检索对应于d键的值是这样做的: map<char,int,less<char> >::const_iterator ptr; ptr=map1.find('d'); cout<<''\n''<<" "<<(*ptr).first<<" 键对应于值:"<<(*ptr).second; cin.get(); return ; } //从以上例程中,我们可以看到map对象的行为和一般数组的行为类似。Map允许两个或多个值使用比较操作符。
关于multimap:
#include <iostream> #include <map> #include <string> using namespace std; int main(void) { multimap<string,string,less<string> >mulmap; multimap<string,string,less<string> >::iterator p; //初始化多重映射mulmap: typedef multimap<string,string,less<string> >::value_type vt; typedef string s; mulmap.insert(vt(s("Tom "),s("is a student"))); mulmap.insert(vt(s("Tom "),s("is a boy"))); mulmap.insert(vt(s("Tom "),s("is a bad boy of blue!"))); mulmap.insert(vt(s("Jerry "),s("is a student"))); mulmap.insert(vt(s("Jerry "),s("is a beatutiful girl"))); mulmap.insert(vt(s("DJ "),s("is a student"))); //输出初始化以后的多重映射mulmap: for(p=mulmap.begin();p!=mulmap.end();++p) cout<<(*p).first<<(*p).second<<endl; //检索并输出Jerry键所对应的所有的值 cout<<"find Jerry :"<<endl; p=mulmap.find(s("Jerry ")); while((*p).first=="Jerry ") { cout<<(*p).first<<(*p).second<<endl; ++p; } cin.get(); return ; } //在map中是不允许一个键对应多个值的,在multimap中,不支持operator[],也就是说不支持map中允许的下标操作。
STL——map的更多相关文章
- 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及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩
问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
- C++ STL map使用
Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map构造函数:map<string , in ...
随机推荐
- 【移动端】js禁止页面滑动与允许滑动
禁止页面滑动 通常静止滑动方案:(阻止滑动事件) window.ontouchmove=function(e){ e.preventDefault && e.preventDefaul ...
- Python cv2 OpenCV 中传统图片格式与 base64 转换
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法.通过http传输图片常常将图片数据转换成base64之后再进行传输. Base64简 ...
- ionic + angular + cordova, 打造专属自己的App!
ionic 学习地址:http://ionicframework.com/ ionic 好处: ionic serve --lab 预览平台间的差异化 sass 提 ...
- 标准库 string
1.func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片 package main import ( " ...
- Linear Regression with PyTorch
Linear Regression with PyTorch Problem Description 初始化一组数据 \((x,y)\),使其满足这样的线性关系 \(y = w x + b\) .然后 ...
- Eclipse中手动清理项目缓存,
用过Eclipse或MyEclipse的小伙伴肯定遇到过这种情况: 代码出错后,在前台访问出问题.然后把代码改好,已经检查不到错误,可是项目在前台访问还是有问题. 这个时候,可能就是Eclipse/M ...
- bzoj 1014 火星人prefix - 链表 - 分块
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- myeclise中创建maven web程序
myeclipse自带了许多插件,因此使用频率很高,但是对maven框架下web程序似乎不是很好的支持,每次创建web程序总是会报一大堆的异常,因此特此记录一下如何在myeclipse下创建一个web ...
- Python3基础 response.info 服务器返回的header信息
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ubuntu18.04无法安装libesd0-dev【学习笔记】
执行如下命令安装: sudo apt-get install libesd0-dev 却报了这个错误: 解决办法: sudo vim /etc/apt/sources.list //在行尾添加如下两行 ...