map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系。
map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较,元素的各项数据可通过键值检索出来。
使用map容器需要头文件包含语句“#include<map>”, map文件也包含了对multimap多重映照容器的定义。 、map创建、元素插入和遍历访问
创建map对象,键值与映照数据的类型由自己定义。在没有指定比较函数时,元素的插入位置是按键值由小到大插入到黑白树中去的,下面这个程序详细说明了如何操作map容器。
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<string,float> m ; //插入元素,按键值的由小到大放入黑白树中
m["Jack"] = 98.5 ;
m["Bomi"] = 96.0 ;
m["Kate"] = 97.5 ; //先前遍历元素
map<string,float> :: iterator it ;
for(it = m.begin() ; it != m.end() ; it ++)
{
cout << (*it).first << " : " << (*it).second << endl ;
} return ;
} 运行结果:
Bomi :
Jack :98.5
Kate :97.5
程序编译试,会产生代号为“warning C4786” 的警告, “” 是标记符超长警告的代号。可以在程序的头文件包含代码的前面使用"#pragma waring(disable:4786)" 宏语句,强制编译器忽略该警告。4786号警告对程序的正确性和运行并无影响。
、删除元素
map映照容器的 erase() 删除元素函数,可以删除某个迭代器位置上的元素、等于某个键值的元素、一个迭代器区间上的所有元素,当然,也可使用clear()方法清空map映照容器。
下面这个程序演示了删除map容器中键值为28的元素:
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<int, char> m ;
//插入元素,按键值的由小到大放入黑白树中
m[] = 'm' ;
m[] = 'k' ;
m[] = 'x' ;
m[] = 'a' ;
//删除键值为28的元素
m.erase() ;
//向前遍历元素
map<int, char> :: iterator it ;
for(it = m.begin() ; it != m.end() ; it ++)
{
//输出键值与映照数据
cout << (*it).first << " : " << (*it).second << endl ;
}
return ;
} 运行结果:
: x
: m
: a
、元素反向遍历
可以用反向迭代器reverse_iterator反向遍历map映照容器中的数据,它需要rbegin()方法和rend()方法指出反向遍历的起始位置和终止位置。
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<int, char> m ;
//插入元素,按键值的由小到大放入黑白树中
m[] = 'm' ;
m[] = 'k' ;
m[] = 'x' ;
m[] = 'a' ;
//反向遍历元素
map<int, char> :: reverse_iterator rit ;
for( rit = m.rbegin() ; rit != m.rend() ; rit ++)
{
//输入键值与映照数据
cout << (*rit).first << " : " << (*rit).second << endl ;
}
return ;
} 运行结果:
: a
: k
: m
: x
、元素的搜索
使用find()方法来搜索某个键值,如果搜索到了,则返回该键值所在的迭代器位置,否则,返回end()迭代器位置。由于map采用黑白树数据结构来实现,所以搜索速度是极快的。
下面这个程序搜索键值为28的元素:
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<int, char> m ;
//插入元素,按键值的由小到大放入黑白树中
m[] = 'm' ;
m[] = 'k' ;
m[] = 'x' ;
m[] = 'a' ;
map<int, char> :: iterator it ;
it = m.find() ;
if(it != m.end()) //搜索到该键值
cout << (*it).first << " : " << ( *it ).second << endl ;
else
cout << "not found it" << endl ;
return ;
} 、自定义比较函数
将元素插入到map中去的时候,map会根据设定的比较函数将该元素放到该放的节点上去。在定义map的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由小到大的顺序插入元素。在很多情况下,需要自己编写比较函数。
编写方法有两种。
()如果元素不是结构体,那么,可以编写比较函数。下面这个程序编写的比较规则是要求按键值由大到小的顺序将元素插入到map中
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ; //自定义比较函数 myComp
11struct myComp
{
bool operator() (const int &a, const int &b)
{
if(a != b) return a > b ;
else return a > b ;
}
} ;
int main()
{
//定义map对象,当前没有任何元素
map<int, char> m ;
//插入元素,按键值的由小到大放入黑白树中
m[] = 'm' ;
m[] = 'k' ;
m[] = 'x' ;
m[] = 'a' ;
//使用前向迭代器中序遍历map
map<int, char,myComp> :: iterator it ;
for(it = m.begin() ; it != m.end() ; it ++)
cout << (*it).first << " : " << (*it).second << endl ;
return ;
} 运行结果:
:a
:k
:m
:x
()如果元素是结构体,那么,可以直接把比较函数写在结构体内。下面的程序详细说明了如何操作:
#include <map>
#include <string>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
struct Info
{
string name ;
float score ;
//重载 “<”操作符,自定义排列规则
bool operator < (const Info &a) const
{
//按score由大到小排列。如果要由小到大排列,使用“>”号即可
return a.score < score ;
}
} ;
int main()
{
//定义map对象,当前没有任何元素
map<Info, int> m ;
//定义Info结构体变量
Info info ;
//插入元素,按键值的由小到大放入黑白树中
info.name = "Jack" ;
info.score = ;
m[info] = ;
info.name = "Bomi" ;
info.score = ;
m[info] = ;
info.name = "Peti" ;
info.score = 66.5 ;
m[info] = ;
//使用前向迭代器中序遍历map
map<Info,int> :: iterator it ;
for(it = m.begin() ; it != m.end() ; it ++)
{
cout << (*it).second << " : " ;
cout << ((*it).first).name << " : " << ((*it).first).score << endl ;
}
return ;
} 运行结果:
:Bomi
:Peti 66.5
:Jack
、用map实现数字分离
对数字的各位进行分离,采用取余等数学方法是很耗时的。而把数字当成字符串,使用map的映照功能,很方便地实现了数字分离。下面这个程序将一个字符串中的字符当成数字,并将各位的数值相加,最后输出各位的和。
#include <string>
#include <map>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<char, int> m ; //赋值:字符映射数字
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
/*上面的10条赋值语句可采用下面这个循环简化代码编写
27 for(int j = 0 ; j < 10 ; j++)
28 {
29 m['0' + j] = j ;
30 }
31 */
string sa, sb ;
sa = "" ;
int i ;
int sum = ;
for ( i = ; i < sa.length() ; i++ )
sum += m[sa[i]] ;
cout << "sum = " << sum << endl ;
return ;
} 、数字映照字符的map写法
在很多情况下,需要实现将数字映射为相应的字符,看看下面的程序:
#include <string>
#include <map>
#include <iostream>
using std :: cout ;
6using std :: endl ;
7using std :: string ;
8using std :: map ;
int main()
{
//定义map对象,当前没有任何元素
map<int, char> m ; //赋值:字符映射数字
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
m[] = '' ;
/*上面的10条赋值语句可采用下面这个循环简化代码编写
27 for(int j = 0 ; j < 10 ; j++)
28 {
29 m[j] = '0' + j ;
30 }
31 */
int n = ;
string s = "The number is " ;
cout << s + m[n] << endl ;
return ;
} 运行结果:
The number is

重写比较器貌似只能针对key重写,value好像不行

C++ map 映照容器的更多相关文章

  1. map映照容器的使用

    map映照容器可以实现各种不同类型数据的对应关系,有着类似学号表的功能. 今天在做并查集的训练时,就用上了map映照容器. 题目就不上了,直接讲一下用法.顺便说一下,实现过程是在C++的条件下. #i ...

  2. map映照容器

    //map映照容器是由一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系 //map映照容器的键值不允许重复 ,比较函数值对元素 //的键值进行比较,元素的各项数据可通过键值检索出来 ...

  3. 统计频率(map映照容器的使用)

    问题描述  AOA非常喜欢阅读莎士比亚的诗,莎士比亚的诗中有种无形的魅力吸引着他!他认为莎士比亚的诗中之所以些的如此传神,应该是他的构词非常好!所以AOA想知道,在莎士比亚的书中,每个单词出现的频率各 ...

  4. POJ 1002 487-3279(map映照容器的使用)

    Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...

  5. zoj 2104 Let the Balloon Rise(map映照容器的应用)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2104 题目描述: Contest time again! Ho ...

  6. zoj 1109 Language of FatMouse(map映照容器的典型应用)

    题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...

  7. map映照容器(常用的使用方法总结)

    map映照容器的数据元素是由一个键值和一个映照数据组成的,键值和映照数据之间具有一一对应的关系.map与set集合容器一样,不允许插入的元素的键值重复. /*关于C++STL中map映照容器的学习,看 ...

  8. C++STL之map映照容器

    map映照容器 map映照容器的元素数据是由一个键值和一个映照数据组成的, 键值与映照数据之间具有一一映照关系. map映照容器的数据结构也是采用红黑树来实现的, 插入元素的键值不允许重复, 比较函数 ...

  9. multimap多重映照容器(常用的方法总结)

    multimap和map的不同之处在于前者允许重复键值的元素出现. /*关于C++STL中mulitmap的学习,与map不同的是,multimap允许插入重复键值的元素*/ #include < ...

随机推荐

  1. 【转】【WPF】MVVM模式的3种command

    1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand 因为MVVM模式适合于WPF和SL,所以这3种模式中也有一些小差异,比如RelayCo ...

  2. C语言 二级指针内存模型混合实战

    //二级指针内存模型混合实战 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #i ...

  3. U3D rootMotion

    Body Transform The Body Transform is the mass center of the character. It is used in Mecanim's retar ...

  4. Asp.net MVC十问十答[译]

    1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural s ...

  5. ios 定位 监听是否跨入某个指定的区域

    /*****监听用户是否进入和走出 在某个区域*****/ 1 #import "ViewController.h" #import <CoreLocation/CoreLo ...

  6. java从0开始学——数组,一维和多维

    #,在java中,允许数组的长度为0:也就是允许      int[] zeroLenthArray = new int[0]; #,匿名的数组初始化是合法的:     int[] smallPrim ...

  7. java加解密操作过程中的中文乱码问题

    import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import ...

  8. 20145233韩昊辰 《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)

    20145233 <Java程序设计>实验报告一:Java开发环境的熟悉 实验要求 使用JDK编译.运行简单的Java程序: 使用IDEA 编辑.编译.运行.调试Java程序. 实验内容 ...

  9. MFC中对话框类(Dialog)的应用

    转载http://hi.baidu.com/jackywdx/item/feee8041d2c2e12310ee1e85 Windows应用程序通常是通过对话框接收用户输入.向用户输出信息,本节介绍应 ...

  10. Ado.net 通用访问类

    public class DbHelperSQL { private static string connString = ConfigurationManager.ConnectionStrings ...