C++ <initializer_list> - begin() Function
The C++ initializer_list::begin function returns a pointer to the first element of the initializer_list.
Syntax
const T* begin() const noexcept;
constexpr const T* begin() const 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, the initializer_list::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}; initializer_list<int>::iterator it; //printing content of the initializer_list cout<<"ilist contains: "; for(it = ilist.begin(); it != ilist.end(); ++it) cout<<*it<<" "; return 0; }
The output of the above code will be:
ilist contains: 10 20 30 40 50
❮ C++ <initializer_list> Library