DEV Community

Cover image for Quick Interview Crack (Unordered map)
Nihal Islam
Nihal Islam

Posted on • Edited on

Quick Interview Crack (Unordered map)

Unordered map is a data structure in C++, which contains key value pairs. It can contain any type of pairs.

Introduction

Unordered set is implemented as hash table. It creates indices through hash table. As a result, the element inside the map is unordered where in map the keys are inserted in increasing order. And for searching an element in map takes O(logn) time where in unordered map the time complexity of searching an element is O(1).

Why is it different than unordered set?

We can use unordered set when we only work with integers because unordered map contains value by indexes but we can set key value pair of any type in unordered map which is quite helpful to solve code problems in less time. Some of the common methods of unordered map are insert, delete, search etc.

How to use unordered map?

#include<unordered_map>

unordered_map<string, string> umap;

//inserting an element
umap["Fruit"] = "Apple";
umap["Flower"] = "Lily";

cout << umap["Flower"] << endl;
//Output Lily
Enter fullscreen mode Exit fullscreen mode

Top comments (0)