map 是以 pair形式插入的。

map中的元素的类型value_type
typedef pair<const Key, Type> value_type;
value_type 被声明为 pair <const key_type, mapped_type> 但并不是简单的 pair <key_type, mapped_type> 因为用一个非常量的迭代器或引用不能改变关联容器的Key。

#include <map>
#include <iostream>
int main( )
{
using namespace std;
typedef pair <const int, int> cInt2Int;
map <int, int> m1;
map <int, int> :: key_type key1;
map <int, int> :: mapped_type mapped1;
map <int, int> :: value_type value1;
map <int, int> :: iterator pIter;
// value_type can be used to pass the correct type
// explicitly to avoid implicit type conversion
m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
// Compare other ways to insert objects into a map
m1.insert ( cInt2Int ( 2, 20 ) );
m1[ 3 ] = 30;
// Initializing key1 and mapped1
key1 = ( m1.begin( ) -> first );
mapped1 = ( m1.begin( ) -> second );
cout << "The key of first element in the map is "
<< key1 << "." << endl;
cout << "The data value of first element in the map is "
<< mapped1 << "." << endl;
// The following line would cause an error because
// the value_type is not assignable
// value1 = cInt2Int ( 4, 40 );
cout << "The keys of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
}
Output
The key of first element in the map is 1.
The data value of first element in the map is 10.
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.

c++如何理解map对象的value_type是pair类型的更多相关文章

  1. js对象详解(JavaScript对象深度剖析,深度理解js对象)

    js对象详解(JavaScript对象深度剖析,深度理解js对象) 这算是酝酿很久的一篇文章了. JavaScript作为一个基于对象(没有类的概念)的语言,从入门到精通到放弃一直会被对象这个问题围绕 ...

  2. 【转】让EntityManager的Query返回Map对象

    在JPA 2.0中我们可以使用entityManager.createNativeQuery()来执行原生的SQL语句.但当我们查询结果没有对应实体类时,需使用entityManager.create ...

  3. 让JPA的Query查询接口返回Map对象

    在JPA 2.0 中我们可以使用entityManager.createNativeQuery()来执行原生的SQL语句. 但当我们查询结果没有对应实体类时,query.getResultList() ...

  4. 理解Java对象:要从内存布局及底层机制说起,话说….

    前言 大家好,又见面了,今天是JVM专题的第二篇文章,在上一篇文章中我们说了Java的类和对象在JVM中的存储方式,并使用HSDB进行佐证,没有看过上一篇文章的小伙伴可以点这里:<类和对象在JV ...

  5. 深入理解javascript对象系列第三篇——神秘的属性描述符

    × 目录 [1]类型 [2]方法 [3]详述[4]状态 前面的话 对于操作系统中的文件,我们可以驾轻就熟将其设置为只读.隐藏.系统文件或普通文件.于对象来说,属性描述符提供类似的功能,用来描述对象的值 ...

  6. Java中如何遍历Map对象的4种方法

    在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...

  7. js 操作map对象

    转自:http://smallvq123.javaeye.com/blog/823923 /* * Map对象,实现Map功能 * * * size() 获取Map元素个数 * isEmpty() 判 ...

  8. 转!! Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  9. JavaScript创建Map对象(转)

    JavaScript 里面本身没有map对象,用JavaScript的Array来实现Map的数据结构. /* * MAP对象,实现MAP功能 * * 接口: * size()     获取MAP元素 ...

随机推荐

  1. 事件 on

    $(选择器).on(事件名称,事件的处理函数) 事件名称:js事件去掉on的部分,例如js中onclick,这里就是click 例如:<input type="button" ...

  2. POJ1979_Red and Black(JAVA语言)

    思路:bfs裸题. 对这种迷宫问题的bfs,我们把坐标点用一个class来存储,并放入队列进行求解. //一直接收不了输入,找了一个多小时的问题,居然是行和列搞反了ORZ Red and Black ...

  3. 你说,怎么把Bean塞到Spring容器?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 小傅哥,你是怎么学习的? 有很多初学编程或者码了几年CRUD砖的小伙伴问我,该怎么学 ...

  4. springboot的拦截器报错plicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]

    解决方案: spingboot的拦截器"index.html"少"/",太粗心了

  5. 「HTML+CSS」--自定义加载动画【010】

    前言 Hello!小伙伴! 首先非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 哈哈 自我介绍一下 昵称:海轰 标签:程序猿一只|C++选手|学生 简介:因C语言结识编程,随后转入计算机 ...

  6. Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送

    实现Gitlab事件自动触发Jenkins构建及钉钉消息推送 实践环境 GitLab Community Edition 12.6.4 Jenkins 2.284 Post build task 1. ...

  7. java面试系列<2>——java容器

    1.概览 容器主要包括Collection和Map两种,Collection存储着对象的集合,而map存储着键值对(两个对象)的映射表 Collection 1.set TreeSet:基于红黑树实现 ...

  8. 交换机之间的通信 VLAN和trunk

    只有 PC0和PC2可通信,PC1和PC3可通信 将PC0和PC2加入同一个VLAN 将PC1和PC3加入同一个VLAN 将左边的交换机的Fa0/3口开启trunk模式即可(如下图)

  9. JVM调优基础到进阶

    GC和GC Tuning GC的基础知识 1.什么是垃圾 C语言申请内存:malloc free C++: new delete c/C++ 手动回收内存 Java: new ? 自动内存回收,编程上 ...

  10. k8s 使用nfs做provisioner

    官方的nfs provisoner,serviceAccount RABC相关 请在这里下载https://raw.githubusercontent.com/kubernetes-incubator ...