C++ List Library - size() Function


Advertisements

Description

The C++ function std::list::size() returns the total number of elements present in the list.

Declaration

Following is the declaration for std::list::size() function form std::list header.

C++98

size_type size() const;

C++11

size_type size() const noexcept;

Parameters

None

Return value

Returns the number of elements present in the list.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::list::size() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l;

   cout << "Initial size of list = " << l.size() << endl;

   l.resize(5);

   cout << "size of list after resize operation = " << l.size() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Initial size of list = 0
size of list after resize operation = 5
list.htm
Advertisements