map就是映射。

定义

map<typename,typename>

注:map的元素是pair。

特性

map会对第一个对象自动排序。

map不允许有两个相同的关键字。

map可以定义迭代器iterator。当然,map相当于一个像pair的结构体,要访问元素时注意使用->或者(*it).first/second。

first是key,second才是value。

typename也可以是结构体。

关键字

erase(iterator)

insert(pair(typename,typename)

find(typename(first)) 返回值为地址。

count(typename)查询元素次数。

栗子:记录学生姓名和成绩,通过姓名查找成绩

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<utility>
#include<map>
#include<string>
using namespace std;
map <string,int>q;
const int maxn=5;
inline int read()
{
int x=0,w=0;char c=getchar();
while(!isdigit(c))w|=c=='-',c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
return w?-x:x;
}
int main()
{
int n=read();
string c;
for(int i=1;i<=n;i++)
{
cin>>c;
q.insert(make_pair(c,read()));
}
int m=read();
for(int i=1;i<=m;i++)
{
cin>>c;
printf("%d\n",q[c]);
}
return 0;
}

map 和 unordered_map的更多相关文章

  1. map 与 unordered_map

    两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...

  2. map和unordered_map的差别和使用

    map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...

  3. 【转】Map 与 Unordered_map

    map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...

  4. C++ map与unordered_map

    map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...

  5. STL中的map和unordered_map

    STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...

  6. C++中map和unordered_map的用法

    1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...

  7. map和unordered_map使用小结

    map和unordered_map unordered_map简介: #include <cstdio> #include <iostream> #include <un ...

  8. 原 c++中map与unordered_map的区别

    c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...

  9. 关于c++ STL map 和 unordered_map 的效率的对比测试

    本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...

  10. Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序

    写程序时,面临用Map还是unordered_map,总是很纠结,于是写了个程序进行测试 Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序 简单数据(4 Byte) 首先 ...

随机推荐

  1. Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer

    作者:Grey 原文地址:Java IO学习笔记二:DirectByteBuffer与HeapByteBuffer ByteBuffer.allocate()与ByteBuffer.allocateD ...

  2. 【NX二次开发】Block UI 截面构建器

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  3. WPF Frame 的 DataContext 不能被 Page 继承

    转载至https://blog.csdn.net/sinat_31608641/article/details/88914517 已测试解决方案可行,因为WPF相关资料稀少,防止日后404,特搬运到自 ...

  4. Win32Api -- 使应用Always on top的几种方法

    本文介绍几种使应用一直置于顶层的方法. 问题描述 一般情况下,想要将应用置于顶层,设置其TopMost属性为true即可.对于多个设置了TopMost属性的应用,后激活的在上面. 但有的应用,比如全局 ...

  5. Mysql的5种索引添加类型

    1.添加普通索引: alter table 'table_name' add index index_name('column') 2.添加主键索引 alter table 'table_name' ...

  6. Kubernetes网络的iptables模式和ipvs模式支持ping分析

    1.iptables模式无法ping通原因分析 iptables模式下,无法ping通任何svc,包括clusterip.所有ns下,下面来分析原因: 查看kubernetes的网络模式 curl 1 ...

  7. centos8添加中文语言包

    centos8添加中文语言包 系统:centos8 查看: [root@centos8]# locale -a 不支持中文包,按照centos7的方式安装:yum install kde-l10n-C ...

  8. 20、checkconfig

    chkconfig控制的原理(/etc/init.d/里面设置脚本,在/etc/rc.d/rc..d中设置软连接,通过chkconfig进行管理,同时也加入到了service服务,chkconfig设 ...

  9. HDU 4438 Hunters 区域赛水题

    本文转载于 http://blog.csdn.net/major_zhang/article/details/52197538 2012天津区域赛最水之题: 题意容易读懂,然后就是分情况求出A得分的数 ...

  10. 26 bash shell中的信号

    当没有任何捕获时,一个交互式 Bash Shell 会忽略 SIGTERM(发送到进程的 TERM 信号用于要求进程终止) 和 SIGQUIT(当用户要求进程执行 core dump 时,QUIT 信 ...