Notice

  1. 如果是program中有两个map对象,可能你需要两个map iterator,但是注意两个iter object不能命名一样,可以分别为iter1, iter2

Example

#include <iostream>
#include <map> using namespace std; struct Triple
{
int color [3];
}; struct student
{
string name;
int age;
}; int main()
{
/* Array as a map's value */
Triple red = {1, 0, 0}, green = {0, 1, 0}, blue = {0, 0, 1};
std::map<int, Triple> colors;
colors.insert(std::pair<int, Triple>(0, red));
colors.insert(std::pair<int, Triple>(1, green));
colors.insert(std::pair<int, Triple>(2, blue)); map<int, Triple>::iterator it;
for(it = colors.begin(); it != colors.end(); it++) {
printf("%d --- %d\n", it->first, it->second.color[0]);
} student st1 = {"muahao", 20};
student st2 = {"jack", 21}; map<int, struct student> mp;
mp.insert(pair<int, student>(0, st1));
mp.insert(pair<int, student>(1, st2)); map<int, student>::iterator it2;
for (it2 = mp.begin(); it2 != mp.end(); it2++) {
printf("%d-----age:%d \n", it2->first, it2->second.age);
cout << "name:" << it2->second.name << endl;
} return 0;
}
#./v3
0 --- 1
1 --- 0
2 --- 0
0-----age:20
name:muahao
1-----age:21
name:jack

c++ map: 使用struct或者数组做value的更多相关文章

  1. Go_14:GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  2. GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  3. C语言 数组做函数参数不传数组个数的遍历方法

    //数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...

  4. C语言 数组做函数参数退化为指针的技术推演

    //数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...

  5. 【C语言】二维数组做形参

    二维数组有两种形式: ①在栈上:         int a[4][4] = {...}; ②在堆堆上:          int ** a = new int *[4];           for ...

  6. 【面试题003】c数组做为参数退化的问题,二维数组中的查找

    [面试题003]c数组做为参数退化的问题,二维数组中的查找  一,c数组做为参数退化的问题 1.c/c++没有记录数组的大小,因此用指针访问数组中的元素的时候,我们要确保没有超过数组的边界, 通过下面 ...

  7. [转]数组引用:C++ 数组做参数 深入分析

    "数组引用"以避免"数组降阶"(本文曾贴于VCKBASE\C++论坛) 受[hpho]的一段模板函数的启发,特写此文,如有雷同,实在遗憾. 数组降阶是个讨厌的事 ...

  8. C++数组做参数

    首先,看一下下面这段代码: void changearr(int a[],int n){    cout<<sizeof(a)<<endl;         // 输出4}in ...

  9. Map 转换成byte[] 数组

    把Map转换成byte数组,使用 ByteArrayOutputStream和ObjectOutputStream Map<String,String> map = new HashMap ...

随机推荐

  1. android微信开放平台,申请移动应用的应用签名怎样获取

    在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate? t=manage/createMobile&type=app& ...

  2. Matlab得到二值图像中最大连通区域

    有时候要将二值化图像中最大的连通域保存下来.以下函数提供了一种方法: %function [img]=maxLianTongYu(I):求图像中最大的连通域 %输入:I 输入图像 %输出:img 仅包 ...

  3. 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees

    [CF简单介绍] 提交链接:A. Lala Land and Apple Trees 题面: A. Lala Land and Apple Trees time limit per test 1 se ...

  4. [BZOJ 3126] Photo

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3126 [算法] 差分约束系统 注意SPFA判负环的条件应为 : 若所有点入队次数之和 ...

  5. PL/SQL编程基础

    1. PL/SQL块的基础结构 DECLARE /* * 定义部分——定义常量.变量.复杂数据类型.游标.用户自定义异常 */ BEGIN /* * 执行部分——PL/SQL语句和SQL语句 */ E ...

  6. django - request.POST和request.body获取值时出现的情况

    django request.POST / request.body 当request.POST没有值 需要考虑下面两个要求 1.如果请求头中的: Content-Type: application/ ...

  7. [Swift通天遁地]三、手势与图表-(8)制作股市中常用的蜡烛图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. 零基础如何学习Java和web前端

    今天说一下零基础到底能不能学习Java,为什么有的人说学不了呢,那么接下来我为大家揭晓,零基础到底适合不适合学习Java. 零基础学习Java的途径第一个就是看视频,然后就是看书,或者在线下报个培训班 ...

  9. cocos creator学习

    2019-05-30 22:23:27 按照前一节我发的教程做,大概了解了Cocos creator的基本布局 但是你发现你不好写代码(感觉视频没有提) 需要下载VS code软件,在其上进行编辑,教 ...

  10. Elasticsearch如何做到亿级数据查询毫秒级返回?

    阅读本文大概需要 6 分钟. 如果面试的时候碰到这样一个面试题:ES 在数据量很大的情况下(数十亿级别)如何提高查询效率? 这个问题说白了,就是看你有没有实际用过 ES,因为啥?其实 ES 性能并没有 ...