C++ Standard Library C++ STL Library

C++ <initializer_list> - begin() Function



The C++ <initializer_list> begin() function returns a pointer to the first element of the initializer_list.

This function is overloaded begin() function.

C++ begin end

Syntax

template<class T> const T* 
  begin (initializer_list<T> il) noexcept;
template<class T> constexpr const T* 
  begin (initializer_list<T> il) noexcept;

Parameters

No parameter is required.

Return Value

Returns a pointer to the first element of the initializer_list.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, begin() function is used to access and print all elements of an initializer_list.

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

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

  return 0;
}

The output of the above code will be:

ilist contains: 10 20 30 40 50

❮ C++ <initializer_list> Library