每一部分都单独注释的,运行时取消注释,将其他部分注释起来就可以。
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
#include <string>
/*
Boost.Unordered 不要求其中的元素是可排序的, 因为它不会做出排序操作。
在排序操作无足轻重时(或是根本不需要), Boost.Unordered 就很合适了。
为了能够快速的查找元素, 我们需要使用 Hash 值。
Hash 值是一些可以唯一标识容器中元素的数字, 它在比较时比起类似 String 的数据类型会更加有效率。
而 boost::unordered_set 要求其中的元素都要可计算 Hash 值。
c++中 hash部分的参考文档(主要看前面的hash_map部分就可以)
************
https://blog.csdn.net/yousss/article/details/79541543
*/
struct person
{
std::string name;
int age;
person(const std::string &n, int a)
: name(n), age(a)
{
}
/*
***********此处要注意
除了自定义 hash_value() 函数, 自定义的类型还需要支持通过 == 运算符的比较操作。
************注意此函数后面的const, 必须定义为const函数
*/
bool operator==(const person &p) const
{
return name == p.name && age == p.age;
}
/*
重载<< 是出现的问题,非友元函数,会提示<<参数过多
参考文献
https://www.it1352.com/485439.html
https://stackoverflow.com/questions/236801/should-operator-be-implemented-as-a-friend-or-as-a-member-function
*/
friend std::ostream & operator<<(std::ostream& out, const person& p);
};
std::ostream & operator<<(std::ostream& out, const person& p)
{
out << p.name << " " << p.age << std::endl;
return out;
}
/*
Hash 函数的签名必须是: hash_value()。
Hash 值是单纯的数字, 所以函数的返回值为: std::size_t。
Boost C++ 库已经为一些数据类型定义好了 Hash 函数,
比如: std::string。 但对于像 person 这样的自定义类型, 你就需要自己手工定义了。
hash_value() 的实现往往都很简单: 你只需要按顺序对其中的每个属性都调用
Boost 在 boost/functional/hash.hpp 中提供的 boost::hash_combine() 函数就行了。
*/
std::size_t hash_value(person const &p)
{
std::size_t seed = 0;
boost::hash_combine(seed, p.name);
boost::hash_combine(seed, p.age);
return seed;
}
int tset01()
{
/*
***********unordered_set
boost::unordered_set 提供了与 std::set 相似的函数
*/
/*typedef boost::unordered_set<std::string> unordered_set;
unordered_set set;
set.insert("Boris");
set.insert("Anton");
set.insert("Caesar");
for (unordered_set::iterator it = set.begin(); it != set.end(); ++it)
std::cout << *it << std::endl;
std::cout << set.size() << std::endl;
std::cout << set.max_size() << std::endl;
std::cout << (set.find("David") != set.end()) << std::endl;
std::cout << set.count("Boris") << std::endl;*/
/*
*********unordered_map存储pair对
boost::unordered_map 和 std::map 之间并没多大区别
*/
/*typedef boost::unordered_map<std::string, int> unordered_map;
unordered_map map;
map.insert(unordered_map::value_type("Boris", 31));
map.insert(unordered_map::value_type("Anton", 35));
map.insert(unordered_map::value_type("Caesar", 25));
for (unordered_map::iterator it = map.begin(); it != map.end(); ++it)
std::cout << it->first << ", " << it->second << std::endl;
std::cout << map.size() << std::endl;
std::cout << map.max_size() << std::endl;
std::cout << (map.find("David") != map.end()) << std::endl;
std::cout << map.count("Boris") << std::endl;*/
/*
*********unordered_set存储自定义结构
boost::unordered_set 中的 Hash 函数不能识别 person 类型,
*/
typedef boost::unordered_set<person> unordered_set;
unordered_set set;
set.insert(person("Boris", 31));
set.insert(person("Anton", 35));
set.insert(person("Caesar", 25));
for (unordered_set::iterator it = set.begin(); it != set.end(); ++it)
std::cout << *it << std::endl;
system("pause");
return 0;
}