std::unordered_set is an STL container and its introduced in C++11. It provides a functionality of a Set i.e. it can contains the unique elements only. unordered_set stores the elements internally using a Hash Table.

We can declare an unordered_set of a particular type only i.e.

Elements of Type with which an unoredered_set is declared can only be added in it. Also, all elements of Type T should be,

  • Copyable or Movable
  • Comparable

Above requirements are because of the reason that element added in unordered_set will act as both key and value for internal Hash Table. As it’s a key therefore it should be comparable and copy-able.

Also, all elements in the unordered_set are immutable i.e. they can not be modified once inserted. They can only be deleted after the insertion.

Header File Required is,

Creating and using an std::unordered_set of std::string

As all objects of type std::string is copy-able and comparable. So, let’s create an unordered_set of type std::string and use it i.e.

Here, as we have declared an unordered set of type std::string. It means it can only store unique strings. Let’s see a complete example,


Here we added 4 elements in the unoredered_set but only 3 will get added, last insertion will not work because “second” is already inserted in set and unoredered_set contains only the unique elements.




Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.