Related Articles

# unordered_map size() in C++ STL

• Difficulty Level : Medium
• Last Updated : 31 Dec, 2018

The unordered_multimap::size() is a built-in function in C++ Standard Template Library which return’s the number of element int the unordered map.

Syntax:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
`unordered_multimap_name.size()`

Return Value: It returns the number of the element present in the unordered map.

Time complexity:

`Constant i.e. O(1).`

Program 1:

 `// C++ program to demonstrate``// unordered_map size() method`` ` `#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``unordered_map<``char``, ``char``>``        ``n{ { ``'A'``, ``'G'` `},``           ``{ ``'B'``, ``'E'` `},``           ``{ ``'C'``, ``'E'` `},``           ``{ ``'D'``, ``'K'` `},``           ``{ ``'E'``, ``'S'` `} };`` ` `    ``cout << ``"size of map = "``         ``<< n.size() << endl;`` ` `    ``return` `0;``}`
Output:
```size of map = 5
```

Program 2:

 `// C++ program to demonstrate``// unordered_map size() method`` ` `#include ``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``unordered_map ra;`` ` `    ``cout << ``"Initial size of map = "``         ``<< ra.size() << endl;`` ` `    ``ra = {``        ``{ ``"Geeks"``, 1.556 },``        ``{ ``"For"``, 2.567 },``        ``{ ``"Geeks"``, 3.345 },``        ``{ ``"GeeksForGeeks"``, 4.789 },``        ``{ ``"GFG"``, 5.998 }``    ``};`` ` `    ``cout << ``"size of map = "``         ``<< ra.size() << endl;`` ` `    ``return` `0;``}`
Output:
```Initial size of map = 0
size of map = 4
```

My Personal Notes arrow_drop_up