unordered_multimap empty() function in C++ STL

The unordered_multimap::empty() is a built-in function in C++ STL which returns a boolean value. It returns true if the unordered_multimap container is empty. Otherwise, it returns false.

Syntax:

unordered_multimap_name.empty()

Parameters: The function does not accept any parameter.

Return Value: It returns a boolean value which denotes whether a unordered_multimap is empty or not.

Below programs illustrates the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::empty() function
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 1, 2 });
    sample.insert({ 2, 3 });
    sample.insert({ 3, 4 });
    sample.insert({ 5, 6 });
  
    // if not empty then print the elements
    if (sample.empty() == false) {
        cout << "Key and Elements: ";
  
        for (auto it = sample.begin(); it != sample.end(); it++) {
            cout << "{" << it->first << ":" << it->second << "} ";
        }
    }
  
    // container is erased completely
    sample.clear();
  
    if (sample.empty() == true)
        cout << "\nContainer is empty";
  
    return 0;
}

chevron_right


Output:

Key and Elements: {5:6} {3:4} {2:3} {1:2} {1:2} 
Container is empty

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::empty()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample;
  
    // inserts element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'g', 'd' });
    sample.insert({ 'r', 'e' });
    sample.insert({ 'g', 'd' });
  
    // if not empty then print the elements
    if (sample.empty() == false) {
        cout << "Key and elements: ";
  
        for (auto it = sample.begin(); it != sample.end(); it++) {
            cout << "{" << it->first << ":" << it->second << "} ";
        }
    }
  
    // container is erased completely
    sample.clear();
  
    if (sample.empty() == true)
        cout << "\nContainer is empty";
  
    return 0;
}

chevron_right


Output:

Key and elements: {r:e} {g:d} {g:d} {a:b} {a:b} 
Container is empty


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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 :


Be the First to upvote.


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