boost unordered
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的更多相关文章
- unordered容器
1.散列容器(hash container) 散列容器通常比二叉树的存储方式可以提供更高的访问效率. #include <boost/unordered_set.hpp> #includ ...
- 基于BOOST 实现并发服务器框架
一:设计思路 本服务器框架使用 UDP 传输协议,程序柱线程等待客户端数据,并将数组存取队列缓冲区.另外可开启多个工作线程,工作线程可以依据具体项目实现不同的功能 ,例如可以将队列缓冲区中的数据逐个取 ...
- #include <boost/unordered_set.hpp>
boost.unordered在C++标准容器std::set,std::multiset,std::map和std::multimap的基础上多实现了四个容器:boost::unordered_se ...
- 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 ...
- unorder_set<typename T> 学习
转自http://blog.csdn.net/mmzsyx/article/details/8240071 散列容器(hash container): 通常比二叉树的存储方式可以提供更高的访问效率.# ...
- Serializable unordered set
Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...
- c++新特性与boost
<Boost程序库探秘——深度解析C++准标准库>之试读 前一阵子还看到一篇文章,说C#要重蹈C++的覆辙,这里说的C++的覆辙是什么呢?是指C++语言过于臃肿的功能特性,导致学习人员的流 ...
- Boost简介
原文链接: 吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
随机推荐
- spring boot中注册拦截器
拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重 ...
- Apache检查配置文件语法
Windows环境:httpd -t或者: httpd.exe -w -t -f "C:\Apache2.2\conf\httpd.conf" -d "C:\Apache ...
- ruby的DIR.pwd
在ruby 中,以下代码可以获得当前脚本的绝对路径: require 'pathname' puts Pathname.new(__FILE__).realpath 将以上代码保存在test1.rb中 ...
- win2016
slmgr /ipk CB7KF-BWN84-R7R2Y-793K2-8XDDG slmgr /skms kms.03k.org slmgr /ato
- 【ABAP系列】SAP ABAP基础-数据更新至数据库操作解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-数据更新至 ...
- is_numeric漏洞总结
将16进制数据判断为数据,这样在存入数据库后,数据库会把16进制解析成字符串,可能造成二次注入 转载: https://www.jianshu.com/p/e7cf997d6ccb
- pdo getLastInertID()无结果
该函数只返回具有自增约素的表, 不然返回0. 使用exec()得到的是受影响的行数.
- 索引及explain
索引好比书的目录.通过索引能快速的定位到一条数据. 在MySQL中除了B+树索引之外,还有一些其他的索引类型.比如:全文索引.(DB和DD索引叫R树索引).在MySQL cluster中是P树索引,m ...
- hive 分桶及抽样调查
1.分桶的概述 分区提供了一个隔离数据和优化查询的遍历方式.不是所有的数据集都可形成合力的分区 对于一张表或者分区,hive可以进一步组织成桶,也就是更为细粒度的数据范围 分区针对的是数据的存储路径( ...
- 最小生成树,Prim算法实现
最小生成树 所谓最小生成树,就是一个图的极小连通子图,它包含原图的所有顶点,并且所有边的权值之和尽可能的小. 首先看看第一个例子,有下面这样一个带权图: 它的最小生成树是什么样子呢?下图绿色加粗的边可 ...