Open In App

list::empty() and list::size() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.

list::empty()

empty() function is used to check if the list container is empty or not.

Syntax :

listname.empty()
Parameters :
No parameters are passed.
Returns :
True, if list is empty
False, Otherwise

Examples:

Input  : list list{1, 2, 3, 4, 5};
list.empty();
Output : False

Input : list list{};
list.empty();
Output : True

Errors and Exceptions

  1. It has a no exception throw guarantee.
  2. Shows error when a parameter is passed.

CPP




// CPP program to illustrate
// Implementation of empty() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{};
    if (mylist.empty()) {
        cout << "True";
    }
    else {
        cout << "False";
    }
    return 0;
}


Output:

True

Time Complexity: O(1)

Application : Given a list of integers, find the sum of the all the integers.

Input  : 1, 5, 6, 3, 9, 2
Output : 26
Explanation - 1+5+6+3+9+2 = 26

Algorithm

  1. Check if the list is empty, if not add the front element to a variable initialised as 0, and pop the front element.
  2. Repeat this step until the list is empty.
  3. Print the final value of the variable.

CPP




// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    int sum = 0;
    list<int> mylist{ 1, 5, 6, 3, 9, 2 };
    while (!mylist.empty()) {
        sum = sum + mylist.front();
        mylist.pop_front();
    }
    cout << sum;
    return 0;
}


Output:

26
list::size()

size() function is used to return the size of the list container or the number of elements in the list container.

Syntax :

listname.size()
Parameters :
No parameters are passed.
Returns :
Number of elements in the container.

Examples:

Input  : list list{1, 2, 3, 4, 5};
list.size();
Output : 5

Input : list list{};
list.size();
Output : 0

Errors and Exceptions

  1. It has a no exception throw guarantee.
  2. Shows error when a parameter is passed.

CPP




// CPP program to illustrate
// Implementation of size() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    cout << mylist.size();
    return 0;
}


Output:

5

Application : Given a list of integers, find the sum of the all the integers.

Input  : 1, 5, 6, 3, 9, 2
Output : 26
Explanation - 1+5+6+3+9+2 = 26

Algorithm

  1. Check if the size of the list is 0, if not add the front element to a variable initialised as 0, and pop the front element.
  2. Repeat this step until the list is empty.
  3. Print the final value of the variable.

CPP




// CPP program to illustrate
// Application of size() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    int sum = 0;
    list<int> mylist{ 1, 5, 6, 3, 9, 2 };
    while (mylist.size() > 0) {
        sum = sum + mylist.front();
        mylist.pop_front();
    }
    cout << sum;
    return 0;
}


Output:

26

Time Complexity: O(1)

Auxiliary Space: O(1)

Let us see the differences in a tabular form -:

 

list::empty()  list::size()
1.

It is used to return whether the list container is empty

It is used to return the number of elements in the list container.

2.

It does not take any parameters.

It does not take parameters.

3.

Its return type is of boolean.

Its return type is of integer type.

4.

Its complexity is constant.

Its complexity is constant.

5.

Its iterator validity does not change.

Its iterator validity does not changes.



Last Updated : 28 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads