• map 与 unordered_map


    两者效率对比:

    #include <iostream>
    #include <string>
    #include <map>
    #include <unordered_map>
    #include <sys/time.h>
    #include <list>
    
    using namespace std;
    
    template<class T>
    void fun(const T& t, int sum)
    {
        for(int i = 0; i < sum; i++)
            t.find(i);
    }
    
    template<template <class...> class T>
    float init(int sum)
    {
        T<int,int> t;
        for(int i = 0; i < sum; i++)
            t.insert(make_pair(i,i));
        struct timeval begin,end;
        gettimeofday(&begin,NULL);
        fun(t,sum);
        gettimeofday(&end,NULL);
        float time = end.tv_sec-begin.tv_sec + float(end.tv_usec-begin.tv_usec)/1000000;
        cout<<"33[32msum: "<<sum<<"	time: "<< time <<" sec"<<endl;
        return time;
    }
    
    int main(int argc,char** argv)
    {
        list<int> lt;
        for(int i = 1; i <= 10000000; i*=10)
        {
            lt.push_back(i);
        }
        for(auto& item : lt)
            std::cout<<"33[31m"<<init<unordered_map>(item)/init<map>(item) <<"33[0m
    -------------------
    "<<std::endl;
    }

    本机测试结果为(Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz):

    例子:

    #include <iostream>
    #include <string>
    #include <unordered_map>
    #include <map>
    
    struct Test
    {
        int gameid;
        int subgameid;
        int roomid;
    
    //实现 map 的键条件:
        bool operator<(const Test& obj) const
        {
            if(gameid < obj.gameid)
                return true;
            else if(gameid == obj.gameid && subgameid < obj.subgameid)
                return true;
            else if(gameid == obj.gameid && subgameid == obj.subgameid && roomid < obj.roomid)
                return true;
            else
                return false;
        }
    
    //实现 unordered_map 的键条件之一,还有一条件为实现hash算法
        bool operator==(const Test& obj) const
        {
            return gameid == obj.gameid && subgameid == subgameid && roomid == obj.roomid;
        }
    
    };
    
    //第一种方法:
    namespace std
    {
        template <typename T>
        class hash
        {
            public:
                long operator()(const T& o) const { return 0; }
        };
    
        template <> class hash<Test>
        {
          public:
            long operator()(const Test& x) const
            {
                return  x.gameid*100 + x.subgameid*10 + x.gameid;
            }
        };
    }
    
    //第二种方法
    class test_hash
    {
      public:
        long operator()(const Test& x) const
        {
            return  x.gameid*100 + x.subgameid*10 + x.gameid;
        }
    };
    
    int main()
    {
        std::unordered_map<Test,int> m1;
        std::unordered_map<Test,int,test_hash> m2;
        std::map<Test,int> m;
        Test test1{3,3,3};
        Test test2{1,2,4};
        Test test3{1,2,4};
        m.insert(std::make_pair(test1,10));
        m.insert(std::make_pair(test2,20));
        Test test4{3,3,3};
        std::cout<<m.size()<<std::endl;
        std::cout<<m[test4]<<std::endl;
    }
  • 相关阅读:
    Spring Security OAuth2 源码分析
    Spring Security OAuth2 token权限隔离
    Spring Cloud Feign 使用OAuth2
    Spring Security OAuth2 授权码模式
    全链路追踪spring-cloud-sleuth-zipkin
    Spring Security OAuth2 授权失败(401) 问题整理
    使用ShardingJdbc分表
    Kubectl常用命令
    Spring Cloud Zuul实现IP访问控制
    一次非核心接口(信息提示类)被刷引发的思考
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/3448424.html
Copyright © 2020-2023  润新知