zoukankan      html  css  js  c++  java
  • c++ map: 使用struct或者数组做value

    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
    ", 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 
    ", 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
    
  • 相关阅读:
    第一个TS文件
    盒子模型
    不稳定的排序算法
    状态码Status Code
    从输入URL到页面加载完成发生了什么?
    JavaScript中var、let、const的区别
    explain 详解 (转)
    HttpServletRequest对象方法的用法 (转)
    数据一致性理解
    logback 使用详解 (转)
  • 原文地址:https://www.cnblogs.com/muahao/p/8831948.html
Copyright © 2011-2022 走看看