list_empty( ) and list_size( )in C++ STL


In this article we will be discussing the working, syntax and examples of list::empty() and list::size() function in C++ STL.

What is List in STL?

List are containers that allow constant time insertion and deletion anywhere in sequence. List are implemented as doubly linked lists. List allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::empty( )?

list::empty() is an inbuilt function in C++ STL which is declared in header file. This function is used to check the list container is empty(size is 0) or not.

Syntax

List.name.empty( )

Return value

It returns the Boolean expression True if the list is empty, and it returns false if it is not empty.

Example

Input List: 50 60 80 90
Output False
Input List:
Output True

Approach can be followed

  • First we declare the List.

  • Then we print the List.

  • Then we declare empty( ) function.

By using the above approach we can check the list is empty. From above approach we can enter the element in the list for non-empty list.

Example

// C++ code to demonstrate the working of list empty( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   List<int> list = { 55, 84, 38, 66, 67 };
   // print the list
   cout<< “ List: “;
   for( auto x = List.begin( ); x != List.end( ); ++x)
   cout<< *x << “ “;
   // declaring empty( ) function
   If (lisy.empty( )){
      Cout<< “ True”;
   } else {
      cout<< “false”;
   }
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - List: 55 84 38 66 67
Output - false
Input – List:
Output – True

What is list::size( ) function?

list::size() is an inbuilt function in C++ STL which is declared in header file. This function is used to find the size of list. Generally we find the No. of elements in the list.

Syntax

listname.size( )

Returns − It returns the No. of element in the list

Example

Input – List: 5 6 7 8 9 10
Output – 6
Input – W O N D E R S
Output – 7

Approach can be followed

  • First we declare the list.

  • Then we print the list.

  • Then we print the size of list using size( ) function.

By using above approach we can find the size of list.

Example

// C++ code to demonstrate the working of list size( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main( ){
   List<char> list = { ‘M’, ‘A’, ‘R’, ‘C’, ‘H’, };
   cout<< " List: ";
   for( auto x = list.begin( ); x != list.end( ); ++x)
   cout<< *x << " ";
   // using size( ) function to print No. of element in list
   cout<< " Size of List" << list.size( );
   return 0;
}

Output

If we run the above code then it will generate the following output

Input – List: M A R C H
Output – Size of List: 5

Updated on: 05-Mar-2020

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements