std::initializer_list in C++

In C++, the std::initializer_list is a class template that allows us to initialize a lightweight object with a list of values. An initializer list is used to set values to variables, arrays, classes, functions, constructors of classes, and standard containers like vectors in a convenient way.

Syntax

initializer_list<T> name_of_list= { };
  • Braced Initializer is used to construct the object for initializer_list.
  • It is generally implemented as a wrapper over arrays.
  • Unlike standard containers like vectors, copying the object of the initializer list does not copy the entire elements to the copied list. But both the original and copied object of the initializer list contain the same elements.

std::initializer_list in C++ 11

The std::initializer_list class template was added in C++ 11 and contains many built-in functions to perform various operations with the initializer list. It provides member functions like a size(), begin(), end(), and constructor to construct, iterate, and access elements of the initializer list.

To use initializer_list, you need to include the <initializer_list> header in your C++ program.

Similar Reads

std::initializer_list in C++

In C++, the std::initializer_list is a class template that allows us to initialize a lightweight object with a list of values. An initializer list is used to set values to variables, arrays, classes, functions, constructors of classes, and standard containers like vectors in a convenient way....

Examples of std::initializer_list in C++

Example 1:...

Member Functions of std::initializer_list

...

Applications of initializer_list

...

Limitations of initializer_list

The following are some of the commonly used member functions of the std::initialzer_list class:...

Contact Us