Open In App

unordered_map size() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

The unordered_multimap::size() is a built-in function in C++ Standard Template Library which return’s the number of element in 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: 

CPP




// 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;
}


Output:

size of map = 5

Program 2: 

CPP




// 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;
}


Output:

Initial size of map = 0
size of map = 4


Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads