map映照容器
//map映照容器是由一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系
//map映照容器的键值不允许重复 ,比较函数值对元素
//的键值进行比较,元素的各项数据可通过键值检索出来 #include<iostream>
#include<map>
#pragma warning(disable:4786) //强制编译器忽略标记符超长警告
#include<string>
using namespace std;
int main()
{
//定义map对象,当前没有任何元素
map<string,float> m1;
//插入元素,按键值的由小到大放入黑白数中
m1["Jack"]=98.5;
m1["Bomi"]=96.0;
m1["Kate"]=97.5;
//前向遍历元素
map<string,float>::iterator it;
for(it=m1.begin();it!=m1.end();it++)
{
//输出键值与映照数据
cout<<(*it).first<<":"<<(*it).second<<endl;
}
map<int,char> m2;
//插入元素,按键值的由小到大放入黑白树中
m2[]='m';
m2[]='k';
m2[]='x';
m2[]='a';
//删除键值为28的元素
m2.erase();
//前向遍历元素
for(map<int,char>::iterator it=m2.begin();it!=m2.end();it++)
{
//输出键值与映照数据
cout<<(*it).first<<":"<<(*it).second<<endl;
}
//可以使用反向迭代器reverse_iterator反向遍历map映照容器中的数据,需要rbegin()方法和rend()方法指出反向遍历的起始位置和终止位置
for(map<int,char>::reverse_iterator rit=m2.rbegin();rit!=m2.rend();rit++)
{
cout<<(*rit).first<<":"<<(*rit).second<<endl;
}
map<int,char>::iterator fit;
//使用find()方法来搜索某个键值,如果搜到了,返回其迭代器位置,否则,返回end()迭代器位置
fit=m2.find();
if(fit!=m2.end()) //搜索到该值
{
cout<<(*fit).first<<":"<<(*fit).second<<endl;
}
else
{
cout<<"not found it"<<endl;
}
//自定义比较函数myComp
/*struct myComp
{
bool operator()(const int &a,const int &b)
{
if(a!=b) return a>b;
else
return a>b;
}
};
map<int,char,myComp> m3;
//插入元素,按键值的由大到小放入黑白树中
m3[25]='m';
m3[28]='k';
m3[10]='x';
m3[30]='a';
//使用前向迭代器中序遍历map
for(map<int,char,myComp>::iterator it1=m3.begin();it1!=m3.end();it1++);
{
cout<<(*it1).first<<":"<<(*it1).second<<endl;
} */
//元素是结构体,可以直接把比较函数写在结构体内
/*struct Info
{
string name;
float score;
//重载"<"操作符,按score由大到小
bool operator<(const Info &a) const
{
return a.score<score;
}
};
map<Info,int> m4;
//定义结构体变量
Info info;
info.name="Jack";
info.score=60;
m[info]=25;
info.name="Bomi";
info.score=80;
m[info]=10;
info.name="Peti";
info.score=66.5;
m[info]=30;
map<Info,int>::iterator it2;
for(it2=m4.begin();it2!=m4.end();it2++)
{
cout<<(*it2).second<<":";
cout<<((*it2).first).name<<((*it2).first.score<<endl;
}*/
//用map实现数字分离采用取余方法操作耗时,把数字当字符串使用 map映照功能很方便实现了数字分离
map<char,int> m5;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
m5['']=;
/*上面的10条赋值语句也可采用下面这个循环来简化代码编写
for(int j=0;j<10;j++)
{
m['0'+j]=j;
}*/
string sa,sb;
sa="";
int i;
int sum=;
for(i=;i<sa.length();i++)
{
sum+=m5[sa[i]];
}
cout<<"sum="<<sum<<endl;
//数字映照字符的map写法
map<int,char> m6;
for(int j=;j<;j++)
{
m6[j]=''+j;
}
int n=;
string s="The number is ";
cout<<s+m6[n]<<endl;
return ;
}
map映照容器的更多相关文章
- C++ map 映照容器
map映照容器的元素数据是一个键值和一个映照数据组成的,键值与映照数据之间具有一一映照的关系. map映照容器的数据结构是采用红黑树来实现的,插入键值的元素不允许重复,比较函数只对元素的键值进行比较, ...
- map映照容器的使用
map映照容器可以实现各种不同类型数据的对应关系,有着类似学号表的功能. 今天在做并查集的训练时,就用上了map映照容器. 题目就不上了,直接讲一下用法.顺便说一下,实现过程是在C++的条件下. #i ...
- 统计频率(map映照容器的使用)
问题描述 AOA非常喜欢阅读莎士比亚的诗,莎士比亚的诗中有种无形的魅力吸引着他!他认为莎士比亚的诗中之所以些的如此传神,应该是他的构词非常好!所以AOA想知道,在莎士比亚的书中,每个单词出现的频率各 ...
- POJ 1002 487-3279(map映照容器的使用)
Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...
- zoj 2104 Let the Balloon Rise(map映照容器的应用)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2104 题目描述: Contest time again! Ho ...
- zoj 1109 Language of FatMouse(map映照容器的典型应用)
题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...
- map映照容器(常用的使用方法总结)
map映照容器的数据元素是由一个键值和一个映照数据组成的,键值和映照数据之间具有一一对应的关系.map与set集合容器一样,不允许插入的元素的键值重复. /*关于C++STL中map映照容器的学习,看 ...
- C++STL之map映照容器
map映照容器 map映照容器的元素数据是由一个键值和一个映照数据组成的, 键值与映照数据之间具有一一映照关系. map映照容器的数据结构也是采用红黑树来实现的, 插入元素的键值不允许重复, 比较函数 ...
- multimap多重映照容器(常用的方法总结)
multimap和map的不同之处在于前者允许重复键值的元素出现. /*关于C++STL中mulitmap的学习,与map不同的是,multimap允许插入重复键值的元素*/ #include < ...
随机推荐
- picker-view 组件 的value失效问题
首先检查是不是漏了绑定关系 组件内 组件引用 如过还不行就用下面的方法,顺序问题 在给暂时列表赋值之后再对value赋值
- 【译】Focused and Diffuse Modes(专注与发散模式)
Focused and Diffuse Modes ---文章来源:coursera 面对一个问题,当无论如何都想不出办法时,你会怎么做呢?对于僵尸们来说,只需简单地不断用脑袋撞墙即可.然而活生生的大 ...
- linux命令logger使用
先从别的地方抄过来全部的解释,如下: **options (选项):** -d, --udp 使用数据报(UDP)而不是使用默认的流连接(TCP) -i, --id 逐行记录每一次logger的进程I ...
- wireshark抓包获取好友ip,定位所在位置
1.打开wireshark 2.按Ctrl + F 键进行搜索 1,选择搜索 “字符串”; 2,选择搜索 “分组详情”; 3,填写搜索数据 “020048″; 3.对qq好友发起语言或视频通话(需要对 ...
- 老男孩python学习自修第十五天【常用模块之time】
例如: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import time if __name__ == "__main__": pr ...
- composer 出现You are running Composer with SSL/TLS protection disabled.
开启php的ssl开启 composer config -g -- disable-tls false
- Python——进程通信之间数据共享
from multiprocessing import Manager,Process,Lock def main(dic,lock): lock.acquire() dic['count'] -= ...
- Mybatis -SqlMapConfig.xml环境配置
SqlMapConfig.xml的配置内容和顺序如下(顺序不能乱): Properties(属性) Settings(全局参数设置) typeAliases(类型别名) typeHandlers(类型 ...
- codeforces732C
Sanatorium CodeForces - 732C Vasiliy spent his vacation in a sanatorium, came back and found that he ...
- codeforces618B
Guess the Permutation CodeForces - 618B Bob has a permutation of integers from 1 to n. Denote this p ...