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;
    }


  • 相关阅读:
    遗传基因有多大?
    【Unity3D】【NGUI】怎样动态给EventDelegate加入參数
    怎样通过Java程序提交yarn的mapreduce计算任务
    Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)
    java多线程----拒绝策略
    java多线程-----volatile
    java多线程----JUC集合”01之 框架
    java多线程----Semaphore信号量
    java多线程---CyclicBarrier
    java多线程----CountDownLatch
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9037678.html
Copyright © 2011-2022 走看看