Boost - unordered_set 教程/示例/有什么吗?

发布于 2024-10-07 02:52:48 字数 154 浏览 0 评论 0原文

我想在项目中使用 unordered_set

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

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

谢谢!

I'd like to use unordered_set in a project.

However, documentation for it is either incomplete or just a technical reference, no examples.

Can anyone provide links to online resources which deal with it? Books also welcome, preferably free. Google searching returned nothing of value.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你又不是我 2024-10-14 02:52:48

最常见用例的代码:

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

Code for the most common use case:

#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;
}

Output

found red
found blue

More Information

http://www.cplusplus.com/reference/unordered_set/unordered_set/find/

朦胧时间 2024-10-14 02:52:48

关于它的文档很少,因为它的行为与 std::set,但它需要散列和等于函数而不是比较函数。只需查找 std::set 的示例,并将其替换为 std::unordered_set 你应该没问题。

如果您需要编写哈希函数,文档中有示例,即 这个

There's little docs on it because it behaves exactly like std::set, with the exception that it requires a hashing and equals function instead of a comparison function. Just look up examples for std::set, and replace them with std::unordered_set and you should be fine.

If you need to write a hashing function, there are examples in the docs, i.e. this one.

新雨望断虹 2024-10-14 02:52:48

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

The boost containers are effectively an implementation of the interface first specified by the C++ Standard Library Technical Report (known as TR1), as mentioned in the boost docs. They seem to be part of the new standards working draft by now. Google turns up some more documentation/examples if you search for tr1 and unordered_set. I like the MSDN reference, which also has some samples:

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

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

抹茶夏天i‖ 2024-10-14 02:52:48

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

I would try using the same methods of access that you use on std::set or other containers, http://www.boost.org/doc/libs/1_37_0/doc/html/unordered.html seems to agree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文