zoukankan      html  css  js  c++  java
  • 非关联容器|hash|unordered_map/multimap,unordered_set/multiset

    unordered_map & unordered_multimap
    #include<iostream>
    #include<unordered_map>
    #include<string>
    #include<iterator>
    using namespace std;
    int main()
    {
            pair<int,string> arr[5]={
                    pair<int,string>(1,"北京"),
                    pair<int,string>(2,"上海"),
                    pair<int,string>(2,"昆明"),
                    pair<int,string>(4,"重庆"),
                    pair<int,string>(3,"天津")
            };
            unordered_map<int ,string> test1(arr,arr+5);
            unordered_map<int ,string>::iterator it = test1.begin();
            for(;it!=test1.end();++it)
            {
                    cout<<it->first<<" "<<it->second<<endl;
            }
            //无序map,不能使用less<>和greater<>来排序了
            cout<<"test unordered_multimap:"<<endl;
            unordered_multimap<int ,string> test2(arr,arr+5);
            for(auto& elem:test2)
            {
                    cout<<elem.first<<" "<<elem.second<<endl;
            }
            cout<<"test random access:"<<endl;
            cout<<test1[1]<<endl;  // unordered_multimap不支持随机访问
            unordered_multimap<int,string>::iterator umit = test2.find(2);
            cout<<umit->first<<" "<<umit->second<<endl;
    }

    unordered_set & unordered_multiset
    #include<iostream>
    #include<unordered_set>
    using namespace std;
    int main()
    {
            int arr[7] = {2,2,1,3,4,5,3};
            unordered_set<int> test1(arr,arr+7);
            for(auto& elem:test1)
                    cout<<elem<<" ";
            cout<<endl;
            cout<<"test unordered_multiset"<<endl;
            unordered_multiset<int> test2(arr,arr+7);
            for(auto& elem:test2)
                    cout<<elem<<" ";
            cout<<endl;
            unordered_multiset<int> test3(arr,arr+7);
            cout<<"test random access"<<endl;
            cout<<*(test2.find(3))<<endl;
            cout<<*(test2.find(2))<<endl;
            cout<<"value = 2 cnt:"<<test2.count(2)<<endl;
            return 0;
    }


  • 相关阅读:
    NSGA3理解(NSGA3算法及其MATLAB版本实现)
    基于分解的多目标进化优化MOEA/D之切比雪夫方法代码
    基于分解的多目标进化优化MOEA/D三种聚合函数的理解
    NSGA-II in MATLAB 源码中文注释(1)(转载)
    我的个人总结
    Unity Networking API文档翻译(二):The High Level API
    Unity Networking API文档翻译(一):Networking概述
    Unity3D独立游戏开发日记(二):摆放建筑物
    Unity3D独立游戏开发日记(一):动态生成树木
    如何申请TexturePacker
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9037678.html
Copyright © 2011-2022 走看看