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 ...
随机推荐
- leetcode_Product of Array Except Self
描写叙述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- Erlang下与其他程序和语言的通信机制(2)
前面聊了普通端口,今天聊下链入式驱动端口,以及NIFs. 链入式驱动端口 如上图所示,链入式驱动端口与Erlang虚拟机存在于同一个OS进程中. 在Erlang这边与普通端口类似,所有与链入式驱动端口 ...
- Python学习笔记9:标准库之日期时间(time包,datetime包)
一 time包 sleep([float time]) 延迟一段以浮点数表示的秒数 time包基于C语言的库函数(library functions). Python的解释器一般是用C编写的,Pyth ...
- Java与设计模式-适配器模式
适配器模式是开发中常常会用到的模式,Android开发中常常常使用到的各种adapter就属于适配器模式,连接各种数据库时也要用到适配器模式. 适配器模式在生活中的实例也随处可见,你托人在日本买了个电 ...
- mp3播放时间
import os os_sep = os.sep this_file_abspath = os.path.abspath(__file__) this_file_dirname, this_file ...
- web中servletcontext和applicationContext
参看:http://www.cnblogs.com/isisbenben/p/5594323.html servletContext和ApplicationContext有什么区别 servletCo ...
- Fiddler抓取https请求,解决“证书错误”警告
要抓取走HTTPS内容,Fiddler必须解密HTTPS流量. 但是,浏览器将会检查数字证书,并发现会话遭到窃听.为了骗过浏览 器,Fiddler通过使用另一个数字证书重新加密HTTPS流量. Fid ...
- 数组和字符串长度length
数组.length 字符串.length() list list.size();map map.size();set set.size();1 java中的length属性是针对数组说的,比如说你声明 ...
- luogu 3834 【模板】可持久化线段树 1(主席树)
我这种菜鸡还是%一下棒神比较好 #include<iostream> #include<cstdio> #include<cmath> #include<cs ...
- yum 安装redis
1.yum install redis 2.yum install php-redis 3service redis start chkconfig redis on