• 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
    
  • 相关阅读:
    vue生命周期详细解析
    Chrome浏览器中onunload有时候没反应怎么办
    JavaScript中<button>与<input type="button"..的区别
    java泛型
    hashCode与equals
    HttpClient HttpServlet HttpUrlConnection
    think in java 笔记
    红黑树
    AC自动机
    并查集
  • 原文地址:https://www.cnblogs.com/muahao/p/8831948.html
Copyright © 2020-2023  润新知