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. UVa 11362 - Phone List

    题目:给你一组电话号码,推断是否有一些号码是其它的前缀(或相等). 分析:字符串.字典树.利用字典树储存查询就可以,注意两种情况处理: 1.先短后长(前缀在前):2.先长后短(前缀在后). 说明:第5 ...

  2. Chrome 消息机制

    Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging 时间:2015-10-08 16:17:59      阅读:1560      评论:0      收藏:0    ...

  3. js 数字格式化,只能输入正负整数,小数

    1.只能输入正整数 <input name="columnValue" class="input96 required" type="text& ...

  4. 20170621_oracle练习

    ========================= 启动Oracle ========================= --->启动OracleOraDb11g_home1TNSListene ...

  5. 20170410 --- Linux备课资料 --- 压缩与解压缩

    这节课我们来学习一下压缩与解压缩,那什么是压缩与解压缩呢? 联想一下Windows系统: 选中文件,右键选择即可 如果压缩,可以选择要压缩的格式,而解压缩直接选择就可以完成了 Linux是通过命令的方 ...

  6. 【bzoj1251】序列终结者(伸展树)

    [bzoj1251]序列终结者(伸展树) Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我 ...

  7. 网络安全中机器学习大合集 Awesome

    网络安全中机器学习大合集 from:https://github.com/jivoi/awesome-ml-for-cybersecurity/blob/master/README_ch.md#-da ...

  8. [Codeforces Round49F] Session in BSU

    [题目链接] http://codeforces.com/contest/1027/problem/F [算法] 二分图匹配 [代码] #include<bits/stdc++.h> #p ...

  9. ubuntu/linuxmint下java环境变量设置

    1.root权限下使用vi或gedit打开/etc目录下的profile文件,末尾加入环境变量. 1)命令: sudo gedit /etc/profile 2)环境变量个人案例: export JA ...

  10. flux,redux,vuex状态集管理工具之间的区别

    一:redux和flux的区别 1)redux是flux中的一个实现 2))在redux中我们只能定义一个store,在flux中我们可以定义多个 3)在redux中,store和dispatch都放 ...