C++ Tutorials

C++ Tutorials

We will learn about set in C++. Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it.

The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. The values always remain in a sorted order. To insert a value into a set, we use the insert function. If that value already exists in the set, it won’t be added.

If it doesn’t exist, it will be inserted and the container will be sorted. Here, I am inserting a few elements into a set and printing the set using iterators. Just like vectors, we can get the first and the last iterator using begin and end function.

Let’s run this code, we can see that all the elements are unique and sorted. We can use the size, max_size, empty functions like we do in vectors. Like this.

Let’s run this code, we can see that the output is as expected. We can remove elements from the set using erase function. There are two ways of doing so, first by directly passing the value to be deleted.

Like, here I am deleting 100. We can also delete elements by passing its iterator. Like, here, I am deleting all elements from beginning till 30. Let’s run this code, we can see that the first 100 is deleted, then all the values less than 30 are deleted.

To clear the entire set, we can use the clear function. Let’s run this code, we can see that the size of the set is 0 now.

To create a set that stores values in reverse, we can do it like this, now the set will work in the similar fashion just the only difference being the values will be stored in a reverse order.

Unordered_set C++

  • insert()
  • begin()
  • end()
  • count()
  • find()
  • clear()
  • cbegin()
  • cend()
  • bucket_size()
  • erase()
  • size()
  • swap()
  • emplace()
  • max_size()
  • empty()
  • equal_range
  • operator=
  • hash_function()
  • reserve()
  • bucket()
  • bucket_count()
  • load_factor()
  • rehash()
  • max_load_factor()
  • emplace_hint()
  • == operator
  • key_eq()
  • max_bucket_count()

Iterate Through Unordered_set C++

Use the new range-based for loop:

std::unordered_set<T> mySet;
for (const auto& elem: mySet) {
    /* ... process elem ... */
}

Or, you can use the more traditional iterator-based loop:

std::unordered_set<T> mySet;
for (auto itr = mySet.begin(); itr != mySet.end(); ++itr) {
    /* ... process *itr ... */
}

Unordered_set c++ header file

Standard header library <unordered_set>

<unordered_set>

Unordered set header

Header that defines the unordered_set and unordered_multiset container classes:

Classes

unordered_setUnordered Set (class template )
unordered_multisetUnordered Multiset (class template )

Functions

beginIterator to beginning (function template )
endIterator to end (function template )

Unordered_set c++ documentation

member typedefinitionnotes
key_typethe first template parameter (Key)
value_typethe first template parameter (Key)The same as key_type
hasherthe second template parameter (Hash)defaults to: hash<key_type>
key_equalthe third template parameter (Pred)defaults to: equal_to<key_type>
allocator_typethe fourth template parameter (Alloc)defaults to: allocator<value_type>
referenceAlloc::reference
const_referenceAlloc::const_reference
pointerAlloc::pointerfor the default allocator: value_type*
const_pointerAlloc::const_pointerfor the default allocator: const value_type*
iteratora forward iterator to const value_type* convertible to const_iterator
const_iteratora forward iterator to const value_type*
local_iteratora forward iterator to const value_type* convertible to const_local_iterator
const_local_iteratora forward iterator to const value_type*
size_typean unsigned integral typeusually the same as size_t
difference_typea signed integral typeusually the same as ptrdiff_t

Unordered_map C++

std::unordered_map

template<

    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;

namespace pmr {

    template <class Key,
              class T,
              class Hash = std::hash<Key>,
              class Pred = std::equal_to<Key>>
    using unordered_map = std::unordered_map<Key, T, Hash, Pred,
                              std::pmr::polymorphic_allocator<std::pair<const Key,T>>>;
}

Unordered_set C++ pop

Note that the C++ standard library is intentionally designed so that the various container specifications do not include a “get and remove” function: e.g. for vector, you have back() which returns the value at the end, and you have pop_back() which removes the value at the end, but does not return it.

The reasons for this could well be the content of a separate question.

So what you actually want is a method to obtain an element (e.g. begin() as suggested in the comments), and then to remove it once you’ve gotten it (e.g. erase(iterator) as mentioned in the other answer).

Print elements of unordered_set C++

Print a std::set or std::unordered_set in C++

#include <iostream>
#include <unordered_set>
#include <algorithm>
#include <iterator>
 
void print(std::unordered_set<int> const &s)
{
    std::copy(s.begin(),
            s.end(),
            std::ostream_iterator<int>(std::cout, " "));
}
 
int main()
{
    std::unordered_set<int> s = { 5, 3, 2, 4, 1 };
    print(s);
 
    return 0;
}

Unordered_set vs unordered_map

They are nearly identical. unordered_set only contains keys, and no values. There is no mapping from a key to a value, so no need for an operator[]unordered_map maps a key to a value.

You can use the various find methods within unordered_set to locate things.