Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ list::size function is used to find out the total number of elements in the list.


size_type size() const;
size_type size() const noexcept;


No parameter is required.

Return Value

Number of elements present in the list.

Time Complexity

Constant i.e, Θ(1)


In the below example, the list::size function is used find out the total number of elements in a list called MyList.

#include <iostream>
#include <list>
using namespace std;
int main (){
  list<int> MyList{10, 20, 30, 40, 50};

  cout<<"List size is: "<<MyList.size()<<"\n\n";

  cout<<"Three elements in the List is added.\n";

  cout<<"Now, List size is: "<<MyList.size()<<"\n";
  return 0;

The output of the above code will be:

List size is: 5

Three elements in the List is added.
Now, List size is: 8

❮ C++ - List