Boost.Unordered provides the classes boost::unordered_set, boost::unordered_multiset, boost::unordered_map, and boost::unordered_multimap. These classes are identical to the hash containers that were added to the standard library with C++11.

1. boost::unordered_set

#include <boost/unordered_set.hpp>
#include <string>
#include <iostream> int main() {
boost::unordered_set<std::string> set;
set.emplace("cat");
set.emplace("shark");
set.emplace("spider"); for (const std::string& s : set) {
std::cout << s << std::endl;
} std::cout << set.size() << std::endl;
std::cout << set.max_size() << std::endl; std::cout << std::boolalpha << (set.find("cat") != set.end()) << std::endl;
std::cout << set.count("shark") << std::endl;
return ;
}

输出为:

spider

shark

cat

3

1152921504606846975

true

1

boost::unordered_set can be replaced with std::unordered_set, boost::unordered_set doesn't differ from std::ordered_set.

2. boost::unordered_map

#include <boost/unordered_map.hpp>
#include <string>
#include <iostream> int main() {
boost::unordered_map<std::string, int> map;
map.emplace("cat", );
map.emplace("shark", );
map.emplace("spider", ); for (const auto& p : map) {
std::cout << p.first << ";" << p.second << std::endl;
} std::cout << map.size() << std::endl;
std::cout << map.max_size() << std::endl; std::cout << std::boolalpha << (map.find("cat") != map.end()) << std::endl;
std::cout << map.count("shark") << std::endl;
return ;
}

输出为:

spider;8

shark;0

cat;4

3

1152921504606846975

true

1

3. User-defined type with Boost.Unordered

#include <boost/unordered_set.hpp>
#include <string>
#include <cstddef> struct animal {
std::string name;
int legs;
}; bool operator==(const animal& lhs, const animals& rhs) {
return lhs.name == rhs.name && lhs.legs == rhs.legs;
} std::size_t hash_value(const animal& a) {
std::size_t seed = ;
boost::hash_value(seed, a.name);
boost::hash_value(seed, a.legs);
return seed;
} int main() {
boost::unordered_set<animal> animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); return ;
}

Because the hash function of boost::unordered_set doesn't know the class animal, hash values can't be automatically calculate for elements of this type. That's why a hash function must be defined-otherwise the example can't be compiled.

In adddition to defining hash_value(), you need to make sure two objects can be compared using==. That't why the operator== is overloaded for animal.

boost unordered的更多相关文章

  1. unordered容器

    1.散列容器(hash container)  散列容器通常比二叉树的存储方式可以提供更高的访问效率. #include <boost/unordered_set.hpp> #includ ...

  2. 基于BOOST 实现并发服务器框架

    一:设计思路 本服务器框架使用 UDP 传输协议,程序柱线程等待客户端数据,并将数组存取队列缓冲区.另外可开启多个工作线程,工作线程可以依据具体项目实现不同的功能 ,例如可以将队列缓冲区中的数据逐个取 ...

  3. #include <boost/unordered_set.hpp>

    boost.unordered在C++标准容器std::set,std::multiset,std::map和std::multimap的基础上多实现了四个容器:boost::unordered_se ...

  4. Win10 VS2013 PCL1.8.1和依赖项VTK8.0.1, QHuall(2.15.2), FLANN1.9.1,Boost1.59.0,Zbil1.2.11和libPNG1.6.34编译安装

    编译和安装过程最好使用管理员权限去操作,避免不必要的错误. 一般而言为了区分Debug和Release库,添加输入变量 Name: CMAKE_DEBUG_POSTFIX Type: STRING V ...

  5. unorder_set<typename T> 学习

    转自http://blog.csdn.net/mmzsyx/article/details/8240071 散列容器(hash container): 通常比二叉树的存储方式可以提供更高的访问效率.# ...

  6. Serializable unordered set

    Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...

  7. c++新特性与boost

    <Boost程序库探秘——深度解析C++准标准库>之试读 前一阵子还看到一篇文章,说C#要重蹈C++的覆辙,这里说的C++的覆辙是什么呢?是指C++语言过于臃肿的功能特性,导致学习人员的流 ...

  8. Boost简介

    原文链接:  吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...

  9. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

随机推荐

  1. P1582倒水

    推了一个多小时的式子,ac后一看题解,7行代码搞定 emmmm我还是太菜了 传送 蒟蒻解法: 不管怎么倒水,最终所有瓶子里面的水的数量一定可以用2k表示出来. n最终可以合并成几个瓶子呢? 我们可以把 ...

  2. android7.0对于SharedPreferences设置模式的限制

    错误信息: 03-28 10:16:12.701   830   932 E AndroidRuntime: FATAL EXCEPTION: Thread-903-28 10:16:12.701   ...

  3. MySQL5.7的搭建以及SSL证书

    Centos7 安装MySQL 5.7 (通用二进制包) 1.1  下载软件包 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-l ...

  4. (转载)Spring与SpringMVC父子容器的关系与初始化

    转自 https://blog.csdn.net/dhaiuda/article/details/80026354 Spring和SpringMVC的容器具有父子关系,Spring容器为父容器,Spr ...

  5. java包装类,自动装箱,拆箱,以及基本数据类型与字符串的转换

    package cn.learn; import java.util.ArrayList; /* 包装类 java.lang中,基本运算类型效率高 装箱:把基本类型数据包装为包装类 1.构造方法 In ...

  6. Jmeter中Bean shell脚本格式修改为utf-8

    遇到的问题: 在做 一个发贴的接口测试时发现,发送数字+纯字母贴子时,可以正常请求成功.但当贴内容为中文时,服务端编码为乱码??. 原因: jmeter中,shell脚本的默认的格式为GBK,所以我在 ...

  7. sql片段的定义

    <!-- sql片段 id 表示唯一标示 这里不加where是因为 sql片段只对单表查询才抽取出来 这样的重用性更高 --> <sql id="query_user_wh ...

  8. Python入门之基础day1

    一.引子 1. 什么是编程语言? 编程语言就是人与计算机沟通的介质 2.什么是编程? 编程就是人把自己想命令计算机干的事情通过编程语言翻译出来并写到文件里去 为什么要编程? 编程的目的就是为了让计算机 ...

  9. k8s<------docker

  10. 浅谈随机数发生器(C语言)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/30971395 本文出自:点击打开链接 本来在做数据 ...