简体   繁体   中英

Boost - unordered_set tutorial/examples/ANYTHING?

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!

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/

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, ie this one .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM