TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset empty() function

C++ multiset empty() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset empty()

C++ Multiset empty() function is used to check whether the multiset container is empty or not. It returns true if the multiset container is empty (size is 0) otherwise, it returns false.

Syntax

bool empty() const;               // until C++ 11

bool empty const noexcept;    //since C++ 11

Parameter

None

Return value

The empty() function returns true if the multiset container is empty (size is 0) otherwise, it returns false.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of multiset is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example to check if a multiset contains any element 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

In the above example, initially size of multiset is 0 hence, empty() function returns 1 (true) and after adding elements it returns 0 (false).

Example 2

Let's see a simple example to check whether multiset is empty 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

In the above example, if condition statement is used. If multiset is empty it will return multiset is empty after adding elements it will return 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

In the above example, It simply uses the empty() function in while loop and prints the elements of multiset until the multiset is not empty.

Example 4

Let's see a simple example:

#include 
#include 
#include 

using namespace std;

int main() {

  typedef multiset 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 above example, the program first creates phone multiset interactively with three multiset of numbers. Then it checks if the multiset is empty or not. If multiset is empty, then it displays a message otherwise, it displays all the telephone numbers available in the multiset.

Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf