The Wayback Machine - https://web.archive.org/web/20240130102716/https://www.geeksforgeeks.org/unordered_map-empty-in-c-stl/
Open In App
Related Articles

unordered_map empty in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

unordered_map::empty() function is used to check whether container size is zero or not. If container size is zero then it return TRUE otherwise it return FALSE. 

Syntax: 

 unordered_map_name.empty()

Parameters: This function does not accept any parameter 

Return type: This function returns boolean value TRUE or FALSE. 

Examples: 

Input: ump = { {1, 2}, {3, 4}, {5, 6}, {7, 8}} ump.empty(); 
Output: False 

Input: ump = { }; ump.empty(); 
Output: True

CPP




// CPP program to illustrate
// Implementation of unordered_map empty() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Take any two unordered_map
    unordered_map<int, int> ump1, ump2;
 
    // Inserting values
    ump1[1] = 2;
    ump1[3] = 4;
    ump1[5] = 6;
    ump1[7] = 8;
 
    // Print the size of container
    cout << "ump1 size = " << ump1.size() << endl;
    cout << "ump2 size = " << ump2.size() << endl;
 
    // Running the function for ump1
    if (ump1.empty())
        cout << "True\n";
    else
        cout << "False\n";
 
    // Running the function for ump2
    if (ump2.empty())
        cout << "True\n";
    else
        cout << "False\n";
 
    // Running the function for ump1 after
    // clearing it
    ump1.clear();
    if (ump1.empty())
        cout << "True\n";
    else
        cout << "False\n";
 
    return 0;
}


Output:

ump1 size = 4
ump2 size = 0
False
True
True

Time complexity: O(1).

Auxiliary space: O(n). // n is the total number of elements in the unordered_map.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


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