C++ Standard Library C++ STL Library

C++ <initializer_list> - initializer_list() Function



The C++ initializer_list::initializer_list function is used to construct an empty initializer_list object.

Syntax

initializer_list() noexcept;
constexpr initializer_list() noexcept;

Parameters

No parameter is required.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the initializer_list::size function is used to construct initializer_list objects.

#include <iostream>
#include <initializer_list>
using namespace std;
 
int main (){
  //creating an empty initializer_list
  initializer_list<int> empty_list;
  cout<<"empty_list.size(): "<<empty_list.size()<<"\n";

  //creating an initializer_list using list-initialization
  initializer_list<int> digits{1, 2, 3, 4, 5};
  cout<<"digits.size(): "<<digits.size()<<"\n";

  //using auto for creating an initializer_list, here it
  //means that it will be of type initializer_list<double>
  auto fractions = {10.523, 20.475, 30.785};
  cout<<"fractions.size(): "<<fractions.size()<<"\n";
}

The output of the above code will be:

empty_list.size(): 0
digits.size(): 5
fractions.size(): 3

❮ C++ <initializer_list> Library