0% found this document useful (0 votes)
11 views5 pages

C++ Unordered Map: Usage and Examples

Uploaded by

taqi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

C++ Unordered Map: Usage and Examples

Uploaded by

taqi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

In C++, unordered_map is an unordered associative container that stores data in the form of

unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This
provides average constant-time complexity O(1) for search, insert, and delete operations but
the elements are not sorted in any particular order.

Example:

#include <iostream>

#include <unordered_map>

using namespace std;

int main() {

// Creating an unordered_map with integer

// keys and string values

unordered_map<int, string> um =

{{1, "Geeks"}, {2, "For"}, {3, "C++"}};

for (auto i : um)

cout << [Link] << ": " << [Link]

<< endl;

return 0;

Output

3: C++

1: Geeks

2: For

Explanation: In this example, we created an unordered map um with three key-value pairs: {1,
"Geeks"}, {2, "For"} and {3, "C++"}.
Syntax

Unordered map is defined as the std::unordered_map class template inside the


<unordered_map> header file.

unordered_map<key_type, value_type> um;

where,

 key_type: Data type of key.

 value_type: Data type of value.

 um: Named assigned to the unordered map.

Declaration and Initialization

We can declare and initialize unordered map in different ways as shown:

#include <bits/stdc++.h>

using namespace std;

void print(unordered_map<int, string> um){

for (auto i : um)

cout << [Link] << " " << [Link]

<< endl;

int main() {

// Create an empty unordered_map

unordered_map<int, string> um1;

// Creating an unordered_map using

// initializer list

unordered_map<int, string> um2 =


{{1, "Geeks"}, {2, "For"}, {3, "C++"}};

print(um1);

cout << endl;

print(um2);

return 0;

Output

3 C++

1 Geeks

2 For

Explanation: In this example,

 Statement unordered_map<int, string> um1 creates an empty unordered map. This is


called default initialization.

 Statement unordered_map<int, string> um2 = {{1, "Geeks"}, {2, "For"}, {3, "C+
+"}} initializes the unordered map using an initializer list.

Basic Operations

The basic operations on unordered map are shown below:

1. Inserting Elements

A new key-value pairs can be inserted in unordered map using either []


operator or insert() method. If the element with the given key already exists, the insert()
method skip the insertion but [] operator updates the associated value to the new value.

Example:

#include <bits/stdc++.h>

using namespace std;

int main() {
unordered_map<int, string> um;

// Insert elements using square brackets

um[1] = "Geeks";

// Insert elements using insert() method

[Link]({2, "For"});

[Link]({3, "C++"});

for (auto i : um)

cout << [Link] << ": " << [Link]

<< endl;

return 0;

Output

3: C++

2: For

1: Geeks

2. Accessing Elements

Elements in unordered map can be accessed using the [] operator or at() function. But if the key
is not found, [] operator insert default value for key then return that default value. So, it is
better to use at() method.

Example:

#include <bits/stdc++.h>

using namespace std;


int main() {

unordered_map<int, string> um =

{{1, "Geeks"}, {2, "For"}, {3, "C++"}};

// Access value associated with key 2

// using [] operator

cout << um[2] << endl;

// Access value associated with key 1

// using at() function

cout << [Link](1);

return 0;

Output

For

Geeks

You might also like