Set Empty Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Empty Function

Set Empty Function

BLUF: Mastering Set Empty Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Empty Function

C++ is renowned for its efficiency. Learn how Set Empty Function enables low-level control and high-performance computing in the tutorial below.

C++ set empty

The empty function in C++ is utilized to determine if the set container is devoid of elements. It will provide a true boolean value if the set container has a size of 0, otherwise, it will return false.

Syntax

Example

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

Parameter

Return value

It returns true when the set container is devoid of elements (size equals 0); otherwise, it returns false.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements of a set is considered to be a secure operation.

Exception Safety

This function never throws exception.

Example 1

Let's examine a straightforward example to verify whether a set includes any elements or not:

Example

#include <set>
#include <iostream>
using namespace std;

int main()
{
    set<int> numbers;
    cout << " Initially, numbers.empty(): " << numbers.empty() << "\n";
    numbers = {100, 200, 300};
    cout << "\n After adding elements, numbers.empty(): " << numbers.empty() << "\n";
}

Output:

Output

Initially, numbers.empty(): 1

 After adding elements, numbers.empty(): 0

In the example given, when the set is initially empty with a size of 0, the empty function will return true. However, after adding elements to the set, the function will return false.

Example 2

Let's examine a basic example to verify if a set is empty or not:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {

   set<char> s;

   if (s.empty())
      cout << "Set is empty." << endl;

   s = {100};

   if (!s.empty())
      cout << "Set is not empty." << endl;

   return 0;
}

Output:

Output

Set is empty
Set is not empty

If the aforementioned example uses a conditional statement, when the set is devoid of elements, it will output "set is empty". Conversely, upon adding elements, it will output "set is not empty".

Example 3

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;

  myset = {100, 200, 300};

  while (!myset.empty())
  {
    cout << *myset.begin()<< '\n';
    myset.erase(*myset.begin());
  }

  return 0;
}

Output:

Output

100
200
300

It employs the empty function within a while loop to display the elements of a set until the set is no longer empty.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {

  typedef set<int> phoneSet;
   
   int number;
   phoneSet phone;
   
   if (phone.empty())
      cout << "Set 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 set
   }

   if (!phone.empty())
   {
      cout<<"\nList of telephone numbers: \n";
      phoneSet::iterator p;
      for(p = phone.begin(); p!=phone.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
   }
   return 0;
}

Output:

Output

Set is empty. Please insert content! 
 
Enter three sets of number: 
1111
5555
3333

List of telephone numbers: 
1111 
3333 
5555

In the previous illustration, the software initially generates a phone collection interactively containing three sets of numbers. It subsequently verifies whether the collection is devoid of any entries. Should the collection be empty, a notification is shown; otherwise, it exhibits all the telephone numbers contained within the collection.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience