10

我想unordered_set在一个项目中使用。

但是,它的文档要么不完整,要么只是技术参考,没有示例。

任何人都可以提供处理它的在线资源的链接吗?也欢迎书籍,最好是免费的。谷歌搜索没有返回任何有价值的东西。

谢谢!

4

4 回答 4

10

最常见用例的代码:

#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/

于 2012-12-21T22:07:47.263 回答
7

它的文档很少,因为它的行为与 完全一样std::set,除了它需要散列和等于函数而不是比较函数。只需查找 的示例std::set,并将它们替换为,std::unordered_set您应该没问题。

如果您需要编写散列函数,文档中有示例,即this one

于 2010-12-12T17:32:51.517 回答
4

boost 容器实际上是 C++ 标准库技术报告(称为 TR1)首先指定的接口的实现,如 boost 文档中所述。到目前为止,它们似乎是新标准工作草案的一部分。如果您搜索 tr1 和 unordered_set,Google 会提供更多文档/示例。我喜欢 MSDN 参考,其中也有一些示例:

http://msdn.microsoft.com/en-us/library/bb982739.aspx

http://www.google.de/search?q=tr1+unordered_set

于 2010-12-12T17:47:00.377 回答
2

我会尝试使用您在std::set或其他容器上使用的相同访问方法,http://www.boost.org/doc/libs/1_37_0/doc/html/unordered.html似乎同意。

于 2010-12-12T17:32:43.113 回答