list size() function in C++ STL
Last Updated :
02 Dec, 2022
The list::size() is a built-in function in C++ STL that 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();
Time Complexity – Linear O(1) as per c++11 standard. Possible because the implementation maintains the size field.
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > demoList;
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
int size = demoList.size();
cout << "The list contains " << size << " elements" ;
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...