std::initializer_list in C++ 11 (original) (raw)

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.

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 name_of_list= { };

Examples of std::initializer_list in C++

**Example 1: The below example demonstrates the use of an initializer list in C++.

C++ `

// C++ program to demonstrate the use of initializer list #include #include using namespace std;

int main() { // Initializing an object using initializer_list initializer_list num = { 2, 4, 6, 8, 10, 12 };

// Accessing elements
cout << "Numbers in the list are: ";
for (int it : num) {
    cout << it << " ";
}
return 0;

}

`

Output

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.

C++ `

// C++ program to illustrate the use of initializer_list in // object construction #include using namespace std; #include

// array type container constructed using initializer list template class MyContainer { public: // Constructor taking initializer_list as a parameter MyContainer(initializer_list values) : list(values) { }

// Function to print all elements
void printList() const
{
    for (const T& value : list) {
        cout << value << " ";
    }
    cout << endl;
}

private: initializer_list list; };

// diver code int main() { // Creating an instance of MyContainer with // initializer_list of int type MyContainer intContainer = { 1, 2, 3, 4, 5 }; cout << "Elements of Integer type are: "; intContainer.printList(); cout << endl;

// Creating an instance of MyContainer with
// initializer_list of double type
cout << "Elements of double type are: ";
MyContainer<double> doubleContainer
    = { 1.1, 2.2, 3.3, 4.4, 5.5 };
doubleContainer.printList();
cout << endl;

return 0;

}

`

Output

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.

Member Functions of std::initializer_list

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.

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 using namespace std; #include

void myFunction(initializer_list 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;

}

`

Output

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 using namespace std; #include

int main() { initializer_list list = {1, 2, 3, 4, 5};

// Using range-based for loop
for (int value : list) {
    cout << value << " ";
}

cout << endl;
return 0;

}

`

3. Initialize Standard Containers

The initializer_list can be used for initializing the standard containers with a List of Elementslike 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 #include using namespace std; #include

void printVector(initializer_list 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;

}

`

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 #include #include using namespace std;

initializer_list getNumbers() { return { 1, 2, 3, 4, 5 }; }

int main() { auto num = getNumbers(); // Use the generated numbers for (auto it : num) { cout << it << " "; } return 0; }

`

Limitations of initializer_list

The initializer lists also have some limitations associated with it: