C++ List Library - empty() Function


Advertisements

Description

The C++ function std::list::empty() tests whether list is empty of not. List of zero size is considered as empty.

Declaration

Following is the declaration for std::list::empty() function form std::list header.

C++98

bool empty() const;

C++11

bool empty() const noexcept;

Parameters

None

Return value

Returns true if list is empty otherwise false.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::list::empty() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l;

   if (l.empty())
      cout << "List is empty." << endl;

   l.emplace_back(1);

   if (!l.empty())
      cout << "List is not empty." << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

List is empty.
List is not empty.
list.htm
Advertisements