![]() |
VOOZH | about |
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.
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.
initializer_list<T> name_of_list= { };
Example 1: The below example demonstrates the use of an initializer list in C++.
Numbers in the list are: 2 4 6 8 10 12
Note Member initializer list and initializer_list are not the same. Both are different, it should not be confused with each other.
Example 2: Program to illustrate the use of initializer_list to construct the objects.
Elements of Integer type are: 1 2 3 4 5 Elements of double type are: 1.1 2.2 3.3 4.4 5.5
Explanation: "std::initializer_list" should be used for immediate consumption, such as iterating in a constructor or passing to a function. It must not be stored as a data member because it does not own its storage and its lifetime is limited to the full expression in which it is created.
The following are some of the commonly used member functions of the std::initialzer_list class:
S. No. | Function Name | Description |
|---|---|---|
1 | begin() | Returns a pointer to the first element of the initializer list. |
2 | end() | Returns a pointer to the last element of the initializer list. |
3 | size() | The size() function returns the number of elements present in the initializer list. |
4 | empty() | This function returns true if the initializer list is empty. False otherwise. |
5 | data() | Returns a pointer to the underlying array container. |
Apart from the construction of objects, initializer list can be used in the following cases:
<initializer_list> are used to pass a variable number of arguments to a function.
Example: The below example demonstrates the passing of an initializer list to a function.
Size of myList: 5 Elements of myList: 1 2 3 4 5
The elements of the initializer_list container can be used to store data as it is a lightweight container.
Example: The below example demonstrates the use of range-based loops to access elements of initializer_list.
1 2 3 4 5
The initializer_list can be used for initializing the standard containers with a List of Elements like vectors.
Example: The below example demonstrates the use of initializer_list to initialize standard containers.
1 2 3 4 5
The initializer_list can be used as a return type to return lists from any function. It allows the function to return multiple values.
Example: The below example demonstrates the use of initializer_list as a return type.
1 2 3 4 5
The initializer lists also have some limitations associated with it: