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 ...
随机推荐
- 分页框架pager-taglib学习笔记
说到分页其实可以研究一下我自己项目里面的分页框架的使用. 下面的笔记来自于孔浩老师的视频教程和我自己的开发实践. 使用Pager-taglib可以帮助我们快速开发分页处理. 下载:pager-tagl ...
- openlayers筛选图层
很多时候需要筛选图层,例如选择交互(ol.interaction.Select). 图片来自官方:https://openlayers.org/en/v4.6.5/apidoc/ol.interact ...
- 测开之路七十七:性能测试蓝图之js
//定义全局的editor = nullvar editor = null; //ace_editor的初始化函数function ace_editor() { var editor = ace.ed ...
- 阅读笔记02-读懂HTTPS及其背后的加密原理
1 为什么需要https 使用https的原因其实很简单,就是因为http的不安全. 当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. ...
- [6期]Webshell提权服务器登录
这一期内容较少,分享一点资料给大家吧:https://www.bilibili.com/video/av27708518/?spm_id_from=333.788.b_636f6d6d656e74.9 ...
- mysql 5.1.34
在make之前,将MAKEFILE中的do abi check注释,不要注释名字... mysql 5.1 编译安装 分类: mysql2012-04-06 13:01 17175人阅读 评论(0) ...
- Node.js实战2:模块使用入门。
NodeJS有丰富的三方模块,借助这些模块,可以快速的开发各类应用.这使用Nodejs可以进行很便捷.快速的开发. 1.安装与加载模块内核.三方 使用npm可以搜索.安装.卸载模块. 例: 搜索模块 ...
- IDF-CTF-图片里的英语 writeup
题目链接:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=34 一恒河沙中有三千世界,一张图里也可以有很多东西. ...
- SpringBoot 接口并行高效聚合
转自:juejin.im/post/5d064b90e51d45777540fda7 背景 接口开发是后端开发中最常见的场景, 可能是RESTFul接口, 也可能是RPC接口. 接口开发往往是从各处捞 ...
- eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误
eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误 具体描述: 整体编译能够编译成功,但新加一个java,里面创建一个main方法,运行时,报无法加载主类的错误, 整体编 ...