C++ Unordered_multimap::empty() Function
The C++ std::unordered_multimap::empty() functionis used to check whether unordered_multimap is empty or not. If the unordered_multimap is empty, then it returns true; otherwise, it returns false. This function does not modify or change the content of the container in any way.
Syntax
Following is the syntax of std::unordered_multimap::empty() function.
bool empty() const noexcept;
Parameters
This function does not accepts any parameters.
Return value
This function returns boolean values, i.e.true if unordered_multimap is empty (size zero) otherwise false.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_multimap::empty() function.
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> umm;
if (umm.empty())
cout << "Unordered multimap is empty." << endl;
umm.emplace_hint(umm.begin(), 'a', 1);
umm.emplace_hint(umm.end(), 'b', 2);
if (!umm.empty())
cout << "Unordered multimap is not empty." << endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
Unordered multimap is empty. Unordered multimap is not empty.
Example 2
Consider the following example, where we are going to use the empty() function to check if an unordered_multimap contains any elements or not.
#include <unordered_map>
#include <iostream>
#include <utility>
using namespace std;
int main() {
unordered_multimap<int, int> numbers;
cout << boolalpha;
cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';
numbers.emplace(55, 110);
numbers.insert(make_pair(12345, 115));
cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}
Output
If we run the above code it will generate the following output −
Initially, numbers.empty(): true After adding elements, numbers.empty(): false
Example 3
In the following example, we are going to use the empty() function and display all the elements in the container if it was not empty.
#include <unordered_map>
#include <iostream>
#include <utility>
using namespace std;
int main() {
unordered_multimap<string, int> marks_of_students;
marks_of_students.insert(make_pair("Aman", 100));
marks_of_students.insert(make_pair("Akash", 95));
marks_of_students.insert({{"Vivek", 98},{"Aman", 92},{"Akash", 97},{"Rahul", 96}});
for(auto & it: marks_of_students){
if(marks_of_students.empty()){
cout<<"marks of students are empty, I can't be able to fetch the data!"<<endl;
}
else{
cout<<"Marks of "<<it.first<<" is "<<it.second<<endl;
}
}
cout<<endl;
return 0;
}
Output
Following is the output of the above code −
Marks of Rahul is 96 Marks of Vivek is 98 Marks of Akash is 97 Marks of Akash is 95 Marks of Aman is 92 Marks of Aman is 100