【问题标题】:Boost - unordered_set tutorial/examples/ANYTHING?Boost - unordered_set 教程/示例/任何东西?
【发布时间】:2011-05-24 07:12:42
【问题描述】:

我想在一个项目中使用unordered_set

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

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

谢谢!

【问题讨论】:

    标签: c++ boost stl c++11 unordered-set


    【解决方案1】:

    关于它的文档很少,因为它的行为与std::set 完全一样,只是它需要散列和等于函数而不是比较函数。只需查找 std::set 的示例,然后将它们替换为 std::unordered_set 就可以了。

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

    【讨论】:

    • 它可能有一个与 std::set 非常相似的接口,但它的行为不同。除了散列使其更快的方式之外,Boost 的unordered_set 还可以通过其他方式变慢,例如:svn.boost.org/trac/boost/ticket/3693
    • @John:性能特点不同,不能有序迭代(嗯,叫unordered_set)。否则,它的行为与std::set 完全相同。
    • 擦除性能问题已得到修复:来自 boost 1.61_0 的引用: 注意:在旧版本中,这可能效率低下,因为它必须搜索多个存储桶才能找到返回的迭代器的位置。数据结构已更改,因此不再是这种情况,并且不推荐使用替代擦除方法。
    【解决方案2】:

    最常见用例的代码:

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

    【讨论】:

      【解决方案3】:

      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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2012-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多