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


  • 相关阅读:
    nginx 按天生成日志
    cmder
    EXCEL最大行数问题:org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1700)
    nginx configure 错误记录
    Flume NetCat Demo
    Flume
    hbase
    kafka安装配置
    azkaban
    sqoop
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9335904.html
Copyright © 2011-2022 走看看