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 ...
随机推荐
- 圆周运动的css3特效案例
<!doctype html><html lang="zh-cn"><head> <meta charset="UTF-8&qu ...
- Python分析《武林外传》
我一向比较喜欢看武侠电影.小说,但是06年武林外传开播的时候并没有追剧,简单扫几眼之后发现他们都不会那种飞来飞去的打,一点也不刺激.09年在南京培训的时候,日子简单无聊透顶,大好的周末不能出门,只能窝 ...
- HTML5移动应用——小心代码注入风险
近日在加州举行的移动安全技术大会上,Syracuse大学的研究者的研究报告显示HTML5移动应用可能会给企业带来新的安全风险.开发者的错误可能导致HTML5应用自动执行攻击者通过Wifi蓝牙或短信发送 ...
- Support Vector Machine(4):SMO算法
经过上一篇的推导,我们的优化问题已经化为了如下等价形式: 我们在不考虑任何约束条件的情况下去想这个minimize的问题,可以将其抽象为: SMO算法的思想是,每次迭代,只改变一个参数,而将n-1个参 ...
- PHP/HTML混写的四种方式
[整理]PHP/HTML混写的四种方式 PHP作为一款后端语言,为了输出给浏览器让浏览器呈现出来,无可避免的要输出HTML代码,下文介绍下我用过的三种PHP/HTML混编方法 1.单/双引号包围法 ...
- Deepin 下开启SSH远程登陆
关于deepin下安装ssh以后root用户登陆报错的解决 最近刚刚接触到deepin,觉得,wow,除了mac,还有这么好看的非win系统,而且第测出那个Linux,宽容度很高,非常适合我这种比 ...
- jmeter 非GUI执行测试,没有响应数据保存到jtl文件办法
估计是jmeter为了减轻客户机负担,就没又默认把这些信息保存,如果想要保存,也可以,需要做出如下配置: 修改bin目录下的user.properties文件,追加配置: jmeter.save.sa ...
- Java 不被看好前景堪忧?可能是想多了!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:代码湾 Java发行二十多年来,尤其是在战胜C和C++成为最受程序员喜欢的编程语言之后,一直都是开发者的宠儿. 虽然斯 ...
- Day10---Python的jieba库
jieba库:中文分词第三方库 jieba.lcut(s) jieba.lcut(s,cut_all=true) jieba.lcut_for_search(s)
- Day3---Python的time库的一些简单函数以及用法
time库的一些函数 time.time () : 获取当前时间戳,即计算机内部时间值,浮点数 >>>import time >>> time.time() 1 ...