list size() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.