zoukankan      html  css  js  c++  java
  • unordered_map/unordered_set & unordered_multimap/unordered_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;
    }


  • 相关阅读:
    截取图片组件
    node之mongodb的DAO
    模块化开发插件,组件
    tweenMax实体抛物线
    defineProperties属性的运用==数据绑定
    程序概述
    JavaBase
    [luogu 1092] 虫食算 (暴力搜索剪枝)
    [luogu1073 Noip2009] 最优贸易 (dp || SPFA+分层图)
    [51Nod 1218] 最长递增子序列 V2 (LIS)
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9335904.html
Copyright © 2011-2022 走看看