The empty function in C++ for multimap is employed to verify if the multimap container is devoid of elements. It will yield true if the container is empty (size equals 0); otherwise, it will return false.
Syntax
bool empty() const; // until C++ 11
bool empty const noexcept; //since C++ 11
Parameter
Return value
It returns true when the multimap container is devoid of elements (with a size of 0); otherwise, it returns false.
Complexity
Constant.
Iterator Validity
No changes.
Data races
The container is accessed.
Exception safety
This function never throws exception.
Example 1
Let's examine a basic example to verify whether a multimap includes any element or not:
#include <map>
#include <iostream>
using namespace std;
int main()
{
multimap<int,int> numbers;
cout << " Initially, numbers.empty(): " << numbers.empty() << "\n";
numbers = {
{10, 100},
{20, 200},
{10, 300},
{30, 400},
{20, 200}
};
cout << "\n After adding elements, numbers.empty(): " << numbers.empty() << "\n";
}
Output:
Initially, numbers.empty(): 1
After adding elements, numbers.empty(): 0
Initially, when the multimap's size is 0, the empty function will return true. Subsequently, after adding elements to the multimap, the function will return false.
Example 2
Let's examine a basic instance to verify if the multimap is empty or not:
#include <iostream>
#include <map>
using namespace std;
int main(void) {
multimap<char, int> m;
if (m.empty())
cout << "Multimap is empty." << endl;
m = {
{'a', 10},
{'b', 20}
};
if (!m.empty())
cout << "Multimap is not empty." << endl;
return 0;
}
Output:
Multimap is empty
Multimap is not empty
If the condition statement is employed as shown above, when the multimap is devoid of elements, it will output "multimap is empty". Subsequently, upon insertion of elements, it will indicate that the multimap is now populated.
Example 3
Let's see a simple example:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
mymultimap = {
{'a', 200},
{'a', 100},
{'b', 100}
};
while (!mymultimap.empty())
{
cout << mymultimap.begin()->first << " => " << mymultimap.begin()->second << '\n';
mymultimap.erase(mymultimap.begin());
}
return 0;
}
Output:
a => 200
a => 100
b => 100
In the example above, the empty function is employed within a while loop to display the elements of a multimap until the multimap is no longer empty.
Example 4
Let's see a simple example:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
typedef multimap<string, int> phoneMultimap;
string name;
int number;
phoneMultimap phone;
if (phone.empty())
cout << "Multimap is empty. Please insert content! \n " << endl;
cout<<"Enter three sets of name and number: \n";
for(int i =0; i<3; i++)
{
cin>> name; // Get key
cin>> number; // Get value
phone.insert(phoneMultimap::value_type(name,number));
}
if (!phone.empty())
{
cout<<"\nList of telephone numbers: \n";
phoneMultimap::iterator p;
for(p = phone.begin(); p!=phone.end(); p++)
{
cout<<(*p).first << " " <<(*p).second <<" \n ";
}
}
return 0;
}
Output:
Multimap is empty. Please insert content!
Enter three sets of name and number:
Nikita 1111
Divya 3333
Amita 4444
List of telephone numbers:
Amita 4444
Divya 3333
Nikita 1111
In the provided scenario, the software initially generates a telephone multimap interactively containing three different names. Subsequently, it verifies whether the multimap is devoid of any entries. If the multimap happens to be empty, a notification is shown; otherwise, it presents a comprehensive list of all the names along with their corresponding telephone numbers stored within the multimap.