A quick guide to Unordered Map in C++

Introduction

In STL, there exist containers known as unordered maps that store the items as key-value pairs in any order. Every component has a different key value. Once they are put to the map, keys cannot be modified; they can only be added or removed. However, values may be updated. They are stored in memory as hash tables. The header file with the #include< map> tag contains the map. Iterators can be used to get access to the map's internal components.

Unordered_map Declaration

Syntax:

unordered_map <key_data_type, value_data_type> name {initial_values};

  1. key_data_type: data format for the key that will be kept inside the map
  2. value_data_type: a value to be put inside the map's data type
  3. initial_values:optional argument that sets the map's starting values

Example:

unordered_map <int, string> m; 


unordered_map <int, string> m = {{1, "a"}, {2, "ab"}, {2, "ab"}, {3, "abc"}} 

Functions on unordered_maps

  1. begin(): gives back an iterator for the unordered map's first element.

Parameters: None

Returns ->: iterator

  1. end(): gives an iterator to the next element in the unordered_map after the final element.

Parameters: None

Returns ->: iterator

  1. [key]:returns the value that matches the key supplied.

Parameters: Key

Returns ->: value, associated with the key

  1. size(): It provides the unordered_map size information.

Parameters: None

Returns ->: integer - total number of elements in the unordered_map

  1. insert(pair): In the unordered_map, add a pair.

Parameters: pair to insert

Returns ->: void

  1. erase(key):Take a component out of the unordered_map.

Parameters: The element's removal key or an iterator pointing to the spot where the element should be eliminated

Returns ->: void

  1. find(key): If the key is discovered, it returns an iterator referring to the element; otherwise, it yields an iterator referring to the unordered map's end.

Parameters: element to search

Returns ->: iterator

  1. clear(): The unordered_map whole components are removed.

Parameters: None

Returns ->: void

  1. empty(): It informs us whether or not the unordered_map is empty.

Parameters: None

Returns ->: True if empty, else false

Code

#include<iostream>
#include<unordered_map>

using namespace std;

int main() {
  unordered_map <int, string> m;
  m.insert(pair <int, string> (1, "a"));

  m[2] = "ab";
  m[3] = "abc";
  m[4] = "abcd";

  unordered_map <int, string> ::iterator it;

  cout << "Unordered_map contains\n";
  for (it = m.begin(); it != m.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';

  m.erase(1);
  m.erase(m.find(2));
  cout << "After erasing element, size of unordered_map is " << m.size() << '\n';
  int val = 3;
  if (m.find(val) != m.end())
    cout << "The unordered_map contains " << val << " as key" << endl;
  else
    cout << "The unordered_map does not contains " << val << " as key" << endl;
  cout << "Elements of unordered_map\n";
  for (it = m.begin(); it != m.end(); it++)
    cout << it -> first << " " << it -> second << '\n';
  cout << '\n';

  m.clear();
  if (m.empty() == true) {
    cout << "Unordered_map is empty now!";
  }
  return 0;
}


Output:

Unordered_map contains
4 abcd
3 abc
1 a
2 ab

After erasing element, size of unordered_map is 2
The unordered_map contains 3 as key
Elements of unordered_map
4 abcd
3 abc

Unordered_map is empty now!

write your code here: Coding Playground

Hope this gave you a clear understanding of the concept.