Open In App

How to Find the Size of a List in C++?

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL.

Example:

Input: 
myList = {10, 20, 30, 40, 50};

Output: 
Size of the list is : 5

Find the Size of the List in C++

To find the size of a std::list, we can use the std::list::size() member function of the std::list class to get the size of the list container. It returns the size i.e. number of elements present in the list in the form of an integer.

Syntax to Find Size of List in C++

list_name.size();

C++ Program to Find the Size of a List

The below example demonstrates the use of the std::list::size() function to find the size of a std::list in C++ STL.

C++
// C++ program demonstrate the use of the std::list::size()
// function to find the size of a std::list

#include <iostream>
#include <list>
using namespace std;

int main()
{
    // Creating a list of integers
    list<int> nums = { 10, 20, 30, 40, 50 };

    // Finding the size of the list
    int size = nums.size();

    // Printing the size of the list
    cout << "Size of the list is : " << size << endl;

    return 0;
}

Output
Size of the list is : 5

Time Complexity: O(1)
Auxiliary Space: O(1)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads