zoukankan      html  css  js  c++  java
  • std::map

    • std::map

      • 关联性容器
      • key-value存放方式
      • 不重复key
      • #include <iostream>
        #include <map>
        #include <string>
        
        struct Employee
        {
                Employee(){}
                Employee(const std::string& wszName):Name(wszName){}
                void SetName(std::string newName){ Name = newName;}
                std::string Name;
        };
        
        struct ReverseId:public std::binary_function<int, int, bool>
        {
                bool operator()(const int& key1, const int& key2) const
                {
                        return (key1 <= key2) ? false : true;
                }
        };
        
        int main()
        {
                const int size = 3;
                const std::pair<int, Employee> items[size] =
                {
                        std::make_pair(1,Employee("Tom")),
                        std::make_pair(2,Employee("Jerry")),
                        std::make_pair(3,Employee("Alice")),
                };
        
                std::map<int, Employee, ReverseId> map1(items, items+size);
                //insert
                map1.insert(std::make_pair(4, Employee("Brown")));
                map1[5] = Employee("Fisher");
                //delete        
                std::map<int, Employee, ReverseId>::iterator it = map1.begin(); 
                map1.erase(it); 
        
                Employee& e = map1[2];
                e.SetName("ZhaoHu");
        
                for(it = map1.begin(); it != map1.end(); it++)
                {
                        std::cout<<"key:"<<it->first<<"value:"<<(it->second).Name<<std::endl;
                }
        
                return 0;
        }
            
        

         

    • std::multimap

      • 可重复key
  • 相关阅读:
    [BZOJ 4710] 分特产
    洛谷 P4827 [国家集训队] Crash 的文明世界
    Test 7.12 T2
    [洛谷 P1377] TJOI2011 树的序
    [洛谷 P1013] NOIP1998 提高组 进制位
    ajax2
    Ajax
    javascript下兼容都有哪些
    获取类名 封装 getStyle
    作用域
  • 原文地址:https://www.cnblogs.com/zhaohu/p/9426585.html
Copyright © 2011-2022 走看看