C++ multiset empty
The empty function in C++ Multiset is employed to determine if the multiset container is devoid of elements. When invoked, it yields true if the multiset container is empty (size equals 0); otherwise, it yields false.
Syntax
bool empty() const; // until C++ 11
bool empty const noexcept; //since C++ 11
Parameter
Return value
The empty function evaluates to true when the multiset container has no elements (its size is 0); otherwise, it returns false.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Simultaneously retrieving elements from a multiset is considered a secure operation.
Exception Safety
This function never throws exception.
Example 1
Let's explore a basic example to verify whether a multiset includes any elements or not:
#include <set>
#include <iostream>
using namespace std;
int main()
{
multiset<int> numbers;
cout << " Initially, numbers.empty(): " << numbers.empty() << "\n";
numbers = {100, 200, 300, 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 size of the multiset is 0, the empty function will return 1, indicating true. Once elements are added to the multiset, the empty function will then return 0, indicating false.
Example 2
Let's examine a basic illustration to verify if a multiset is vacant or not:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
multiset<char> s;
if (s.empty())
cout << "Multiset is empty." << endl;
s = {100};
if (!s.empty())
cout << "Multiset is not empty." << endl;
return 0;
}
Output:
Multiset is empty
Multiset is not empty
If the condition statement above is employed, when the multiset is devoid of elements, it will output "multiset is empty"; however, after elements are appended, it will indicate "multiset is not empty."
Example 3
Le's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
mymultiset = {100, 300, 300, 200, 400, 400};
while (!mymultiset.empty())
{
cout << *mymultiset.begin()<< '\n';
mymultiset.erase(*mymultiset.begin());
}
return 0;
}
Output:
100
200
300
400
It employs the empty function within a while loop to display the elements of a multiset until the multiset is devoid of any elements.
Example 4
Let's see a simple example:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
typedef multiset<int> phoneMultiset;
int number;
phoneMultiset phone;
if (phone.empty())
cout << "Multiset is empty. Please insert content! \n " << endl;
cout<<"Enter three sets of number: \n";
for(int i =0; i<3; i++)
{
cin>> number; // Get value
phone.insert(number); // Put them in multiset
}
if (!phone.empty())
{
cout<<"\nList of telephone numbers: \n";
phoneMultiset::iterator p;
for(p = phone.begin(); p!=phone.end(); p++)
{
cout<<(*p)<<" \n ";
}
}
return 0;
}
Output:
Multiset is empty. Please insert content!
Enter three sets of number:
10002
10002
10003
List of telephone numbers:
10002
10002
10003
In the provided example, the software initially generates a phone multiset interactively containing three sets of numbers. Next, it verifies the multiset's status to determine if it contains any elements. If the multiset happens to be empty, a notification is shown; otherwise, it presents all the phone numbers stored within the multiset.