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 ...
随机推荐
- P1297 [国家集训队]单选错位(期望)
P1297 [国家集训队]单选错位 期望入门 我们考虑涂到第$i$道题时的情况 此时题$i$答案有$a[i]$种,我们可能涂$a[i+1]$种 分类讨论: 1.$a[i]>=a[i+1]$: 可 ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- 应使用sqlplus代替tnsping进行oracle连通性测试
一直以来,都习惯于tnsping alias测试确定使用了那个sqlnet.ora,并测试连通性.最近在制作系统的安装包,为了轻量级以及提高实施效率,全部客户端使用oracle instant cli ...
- 11: python递归
1.1 递归讲解 1.定义 1. 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 2.递归特性 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题 ...
- MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案
使用C#的MondoDB驱动,一直没问题.结果最近,MongoCursor的ToList方法,取列表,总是报错 Server instance 127.0.0.1:27017 is no longer ...
- 前端 --- 2 css
一. CSS的几种引入方式 1.行内样式 2.内部样式 写在网页的<head></head>标签对的<style></style>标签对中 3.外部样式 ...
- 使用velocity 小技巧
因为公司的需求,我使用了velocity模板进行文件生成.在这里先记录一下使用velocity模板时的一些小技巧: 1.截取字符串 注意,(1)需要使用.length()获取字符串长度: ...
- Bootstrap3基础 img-responsive 响应式图片
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- P4097 [HEOI2013]Segment(李超树)
链接 https://www.luogu.org/problemnew/show/P4097 https://www.lydsy.com/JudgeOnline/problem.php?id=3165 ...
- CentOS7.2 安装Docker
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看你当前的内核版本 [roo ...