C++ unordered_set

Introduction

The std::unordered_set is a container that allows for constant time insertion, searching, and removal of elements.

This container is implemented as an array of buckets of linked lists.

The hash value of each element is calculated, and the object is placed into an appropriate bucket based on the hash value.

The object themselves are not sorted in any particular order.

To define an unordered set, we need to include the <unordered_set> header.

Example:

#include <iostream> 
#include <unordered_set> 

int main() /*from w  w  w  .j a  va2  s . c o m*/
{ 
    std::unordered_set<int> myunorderedset = { 1, 2, 5, -4, 7, 10 }; 
    for (auto el : myunorderedset) 
    { 
        std::cout << el << '\n'; 
    } 
} 

The values are not sorted but are unique.




PreviousNext

Related