C++ Standard Library C++ STL Library

C++ <initializer_list> - size() Function



The C++ initializer_list::size function is used to find out the total number of elements in the initializer_list.

Syntax

size_t size() const noexcept;
constexpr size_t size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the initializer_list.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the initializer_list::size function is used to find out the total number of elements in the given initializer_list.

#include <iostream>
#include <initializer_list>
using namespace std;
 
int main (){
  initializer_list<int> ilist{10, 20, 30, 40, 50};
  initializer_list<int>::iterator it;

  //printing content of the initializer_list
  cout<<"ilist contains: ";
  for(it = ilist.begin(); it != ilist.end(); ++it)
    cout<<*it<<" ";

  //displaying the size of the initializer_list  
  cout<<"\nSize of ilist is: "<<ilist.size();
  return 0;
}

The output of the above code will be:

ilist contains: 10 20 30 40 50 
Size of ilist is: 5

❮ C++ <initializer_list> Library