Create C++ STL unordered_set

htt‮/:sp‬/www.theitroad.com

In C++, you can create an unordered set using the std::unordered_set container provided by the STL. An unordered set is a container that stores a collection of unique elements in no particular order, and provides fast access to the elements based on their hash value.

Here's an example of how to create an unordered set in C++:

#include <iostream>
#include <unordered_set>

int main() {
  // Create an unordered set of integers
  std::unordered_set<int> mySet = {1, 2, 3, 4, 5};

  // Print the contents of the unordered set
  for (const auto& elem : mySet) {
    std::cout << elem << " ";
  }
  std::cout << std::endl;

  return 0;
}

In this example, we create an unordered set mySet and initialize it with a collection of integers using an initializer list. We then use a ranged for loop to print the values of all the elements in the set.

The output of this program is:

5 4 3 2 1

As you can see, the elements in the set are printed in no particular order.

Note that you can also use the insert() method to add elements to the set, the erase() method to remove elements from the set, and the find() method to search for elements in the set. The unordered set also provides various other methods to check the size of the set, clear the set, etc.