unordered_map size() in C++ STL

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:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// unordered_map size() method
  
#include <iostream>
#include <unordered_map>
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;
}

chevron_right


Output:

size of map = 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// unordered_map size() method
  
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
  
int main()
{
    unordered_map<string, double> 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;
}

chevron_right


Output:

Initial size of map = 0
size of map = 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.