Boost - unordered_set tutorial/examples/ANYTHING?

F. P. picture F. P. · Dec 12, 2010 · Viewed 16.6k times · Source

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!

Answer

Chris Redford picture Chris Redford · Dec 21, 2012

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/