【发布时间】:2011-05-24 07:12:42
【问题描述】:
我想在一个项目中使用unordered_set。
但是,它的文档要么不完整,要么只是技术参考,没有示例。
谁能提供处理它的在线资源的链接?也欢迎书籍,最好是免费的。谷歌搜索没有返回任何有价值的东西。
谢谢!
【问题讨论】:
标签: c++ boost stl c++11 unordered-set
我想在一个项目中使用unordered_set。
但是,它的文档要么不完整,要么只是技术参考,没有示例。
谁能提供处理它的在线资源的链接?也欢迎书籍,最好是免费的。谷歌搜索没有返回任何有价值的东西。
谢谢!
【问题讨论】:
标签: c++ boost stl c++11 unordered-set
关于它的文档很少,因为它的行为与std::set 完全一样,只是它需要散列和等于函数而不是比较函数。只需查找 std::set 的示例,然后将它们替换为 std::unordered_set 就可以了。
如果您需要编写哈希函数,文档中有示例,即this one。
【讨论】:
unordered_set 还可以通过其他方式变慢,例如:svn.boost.org/trac/boost/ticket/3693
unordered_set)。否则,它的行为与std::set 完全相同。
最常见用例的代码:
#include <boost/unordered_set.hpp>
using boost::unordered_set;
using std::string;
using std::cout;
using std::endl;
int main (void)
{
// Initialize set
unordered_set<string> s;
s.insert("red");
s.insert("green");
s.insert("blue");
// Search for membership
if(s.find("red") != s.end())
cout << "found red" << endl;
if(s.find("purple") != s.end())
cout << "found purple" << endl;
if(s.find("blue") != s.end())
cout << "found blue" << endl;
return 0;
}
输出
found red
found blue
更多信息
http://www.cplusplus.com/reference/unordered_set/unordered_set/find/
【讨论】:
boost 容器实际上是 C++ 标准库技术报告(称为 TR1)首次指定的接口的实现,如 boost 文档中所述。到目前为止,它们似乎是新标准工作草案的一部分。如果您搜索 tr1 和 unordered_set,Google 会提供更多文档/示例。我喜欢 MSDN 参考,其中也有一些示例:
【讨论】:
我会尝试使用您在 std::set 或其他容器上使用的相同访问方法,http://www.boost.org/doc/libs/1_37_0/doc/html/unordered.html 似乎同意。
【讨论】: