正在加载……
专注、离线、切勿分心

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


posted on 2018-07-19 15:04  正在加载……  阅读(296)  评论(0编辑  收藏  举报