boost::unordered_map分析和使用(转)

开发C++时,选择合适的数据结构是一个十分重要的步骤。因此,必须对每一个数据结构的原理及应用场景都有所了解。

boost::unordered_map和std::map都是一种关联式容器,且原理类似,通过存储key-value键值对,可通过key快速检索到value,并且key是不重复的。但是,它们之间有一些区别,下面将逐一介绍。

排序区别:

map是有序的:按照operator<比较判断元素的大小,并选择合适的位置插入到树中;
unordered_map是无序的:计算元素的Hash值,根据Hash值判断元素是否相同。
用法区别:

对于key是自定义的类型, map的key需要定义operator<,内置类型(如,int)是自带operator<;
对于key是自定义的类型,unordered_map需要定义hash_value函数并且重载operator==,内置类型都自带该功能。
应用区别:

如果元素需要排序,则使用map;
如果需要高效率和低内存,则使用unordered_map。
————————————————
版权声明:本文为CSDN博主「yz930618」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yz930618/article/details/84783947

 

示例代码一

复制代码
#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

void main(){
    unordered_map<string,int> months;
    //插入数据
    cout<<"insert data"<<endl;
    months["january"]=31;
    months["february"] = 28;
    months["march"] = 31;
    months["september"] = 30;

    //直接使用key值访问键值对,如果没有访问到,返回0
    cout<<"september->"<<months["september"]<<endl;
    cout<<"xx->"<<months["xx"]<<endl;

    typedef unordered_map<int,int> mymap;
    mymap mapping;
    mymap::iterator it;
    mapping[2]=110;
    mapping[5]=220;
    const int x=2;const int y=3;//const是一个C语言(ANSI C)的关键字,它限定一个变量不允许被改变,产生静态作用,提高安全性。

    //寻找是否存在键值对
    //方法一:
    cout<<"find data where key=2"<<endl;
    if( mapping.find(x)!=mapping.end() ){//找到key值为2的键值对
        cout<<"get data where key=2! and data="<<mapping[x]<<endl;
    }
    cout<<"find data where key=3"<<endl;
    if( mapping.find(y)!=mapping.end() ){//找到key值为3的键值对
        cout<<"get data where key=3!"<<endl;
    }
    //方法二:
    it=mapping.find(x);
    printf("find data where key=2 ?  %dn",(it==mapping.end()));
    it=mapping.find(y);
    printf("find data where key=3 ?  %dn",(it==mapping.end()));

    //遍历hash table
    for( mymap::iterator iter=mapping.begin();iter!=mapping.end();iter++ ){
        cout<<"key="<<iter->first<<" and value="<<iter->second<<endl;
    }

    system("pause");
}


示例代码二

static boost::unordered_map<int32, IMessagePtr(*)()> * GetMessageTable()
{
    static std::pair<int32, IMessagePtr(*)()> __DefineMessages[] = { std::pair<int32, IMessagePtr(*)()>(SerializeMessage::RESERVE_IDENTITY, SerializeMessage::Create) };
    static boost::unordered_map<int32, IMessagePtr(*)()> __MessageTable(__DefineMessages, __DefineMessages + sizeof(__DefineMessages) / sizeof(__D efineMessages[0]));
    return &__MessageTable;
}

 

bool MessageFactory::Register(int32 id, IMessagePtr(*creator)())
{
    return core_insert_container(*GetMessageTable(), id, creator);
}

 

template<typename T, typename U, typename V>
inline bool insert_container(std::map<T, U, V> &container, const T &key, const U &value)
{
    return container.insert(std::pair<T, U>(key, value)).second;
}




原文链接: https://www.cnblogs.com/spruce/p/12124573.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    boost::unordered_map分析和使用(转)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/308865

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午6:28
下一篇 2023年2月16日 上午6:28

相关推荐