Applications of initializer_list
Apart from the construction of objects, initializer list can be used in the following cases:
1. Variable Function Parameters
<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.
C++
// C++ program to demonstrate the passing of initializer // list to a function. #include <iostream> using namespace std; #include <initializer_list> void myFunction(initializer_list< int > myList) { // Print the size (length) of myList cout << "Size of myList: " << myList.size(); cout << "\n" ; // Print elements of myList cout << "Elements of myList: " ; // iterate to all the values of myList for ( int value : myList) { // Print value at each iteration cout << value << " " ; } } int main() { // Using initializer list when calling a function myFunction({ 1, 2, 3, 4, 5 }); return 0; } |
Size of myList: 5 Elements of myList: 1 2 3 4 5
2. Store Data in Contigious Memory
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.
C++
// C++ program to demonstrate the use of range based loops to access elements of initializer_list. #include <iostream> using namespace std; #include <initializer_list> int main() { initializer_list< int > list = {1, 2, 3, 4, 5}; // Using range-based for loop for ( int value : list) { cout << value << " " ; } cout << endl; return 0; } |
1 2 3 4 5
3. Initialize Standard Containers
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.
C++
// C++ program to demonstrate the use of initializer_list to // initialize standard containers. #include <iostream> #include <vector> using namespace std; #include <initializer_list> void printVector(initializer_list< int > list) { // initialize vector using initializer_list vector< int > myVector(list); // Printing the elements of vector for ( int value : myVector) { cout << value << " " ; } cout << endl; } int main() { // pass initializer_list to function printVector({ 1, 2, 3, 4, 5 }); return 0; } |
1 2 3 4 5
4. Initializer Lists as Return Types
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.
C++
// C++ program to demonstratethe use of initializer_list as // return type. #include <initializer_list> #include <iostream> #include <vector> using namespace std; initializer_list< int > getNumbers() { return { 1, 2, 3, 4, 5 }; } int main() { auto num = getNumbers(); // Use the generated numbers for ( auto it : num) { cout << it << " " ; } return 0; } |
1 2 3 4 5
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.
Contact Us