Skip to content
Related Articles

Related Articles

Improve Article

list size() function in C++ STL

  • Last Updated : 25 Jun, 2018

The list::size() is a built-in function in C++ STL which is used to find the number of elements present in a list container. That is, it is used to find the size of the list container.

Syntax:

list_name.size();

Parameters: This function does not accept any parameter.

Return Value: This function returns the number of elements present in the list container list_name.

Below program illustrate the list::size() function in C++ STL:




// CPP program to illustrate the
// list::size() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Creating a list
    list<int> demoList;
  
    // Add elements to the List
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
  
    // getting size of the list
    int size = demoList.size();
  
    cout << "The list contains " << size << " elements";
  
    return 0;
}
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :