c++ map: 使用struct或者数组做value
Notice
- 如果是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的更多相关文章
- Go_14:GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- C语言 数组做函数参数不传数组个数的遍历方法
//数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...
- C语言 数组做函数参数退化为指针的技术推演
//数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...
- 【C语言】二维数组做形参
二维数组有两种形式: ①在栈上: int a[4][4] = {...}; ②在堆堆上: int ** a = new int *[4]; for ...
- 【面试题003】c数组做为参数退化的问题,二维数组中的查找
[面试题003]c数组做为参数退化的问题,二维数组中的查找 一,c数组做为参数退化的问题 1.c/c++没有记录数组的大小,因此用指针访问数组中的元素的时候,我们要确保没有超过数组的边界, 通过下面 ...
- [转]数组引用:C++ 数组做参数 深入分析
"数组引用"以避免"数组降阶"(本文曾贴于VCKBASE\C++论坛) 受[hpho]的一段模板函数的启发,特写此文,如有雷同,实在遗憾. 数组降阶是个讨厌的事 ...
- C++数组做参数
首先,看一下下面这段代码: void changearr(int a[],int n){ cout<<sizeof(a)<<endl; // 输出4}in ...
- Map 转换成byte[] 数组
把Map转换成byte数组,使用 ByteArrayOutputStream和ObjectOutputStream Map<String,String> map = new HashMap ...
随机推荐
- 数学之路-python计算实战(1)-ubuntu安装pypy
Get the source code. The following packages contain the source at the same revision as the above bin ...
- 暴力解hdu4930Fighting the Landlords
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- caffe代码阅读10:Caffe中卷积的实现细节(涉及到BaseConvolutionLayer、ConvolutionLayer、im2col等)-2016.4.3
一. 卷积层的作用简单介绍 卷积层是深度神经网络中的一个重要的层,该层实现了局部感受野.通过这样的局部感受野,能够有效地减少參数的数目. 我们将结合caffe来解说详细是怎样实现卷积层的前传和反传的. ...
- YTU 2760: 字符串---首字母变大写
2760: 字符串---首字母变大写 时间限制: 1 Sec 内存限制: 128 MB 提交: 343 解决: 136 题目描述 输入一行英文句子,将每个单词的第一个字母改成大写字母. 输入 一个 ...
- B1277 [HNOI2002]Tinux系统 树形dp
这个题bzoj上没有图,luogu上样例有问题...其实这个题代码不难,但是思考起来还是有一定难度的,其实这些题的重点都在于思考.我就不写了,洛谷上唯一的题解写的挺好,大家可以看一看. 题干: 在do ...
- bzoj3105 [cqoi2013]新Nim游戏——贪心+线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3105 首先,要先手必胜,就不能取后让剩下的火柴中存在异或和为0的子集,否则对方可以取成异或和 ...
- JSP-Runoob:JSP 自动刷新
ylbtech-JSP-Runoob:JSP 自动刷新 1.返回顶部 1. JSP 自动刷新 想象一下,如果要直播比赛的比分,或股票市场的实时状态,或当前的外汇配给,该怎么实现呢?显然,要实现这种实时 ...
- MySQL max_connections 总是 214 。不能设大了? max_connections = 214
MySQL max_connections 总是 214 .不能设大了? centos7 mariadb 修改 max_connections 总是214 It was indeed limits s ...
- PCB WebAPI 接口测试工具与接口文档生成
我们自己写WebAPI或调用对方系统提供的WebAPI时,测试WebAPI接口工具用哪些工具呢. 这里将3种WebAPI常用到的工具使用说明.主要是讲对第3种WebApiTestClientWebAp ...
- poj Code(组合数)
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9918 Accepted: 4749 Description ...