Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 249
F. P. Avatar asked Dec 12 '10 17:12

F. P.


People also ask

Is unordered_set faster than set?

For a small number of elements, lookups in a set might be faster than lookups in an unordered_set . Even though many operations are faster in the average case for unordered_set , they are often guaranteed to have better worst case complexities for set (for example insert ).

What is the use of unordered_set in C++?

The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values).

How does unordered_set know if two elements are equal?

Two unordered_sets are equal if they have the same number of elements and the elements in one container are a permutation of the elements in the other container. Otherwise, they are unequal.

Can unordered_set have duplicate values?

Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container - However, they can be inserted and removed. Unordered sets do not allow duplicates and are initialized using comma-delimited values enclosed in curly braces.


1 Answers

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/

like image 113
Chris Redford Avatar answered Sep 20 '22 20:09

Chris Redford