How to Use Initializer Lists in Constructors in C++? Last Updated : 01 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the initializer list in constructors can be used to initialize member variables with a list of values. In this article, we will learn how to use initializer lists in the constructor in C++. Initializer List in Constructor in C++We can use initializer_lists in constructors for initializing the members of the class with a list of values. Syntax to Declare Initializer Listinitializer_list<type> name;We can declare the argument of the constructor as an initializer_list object and then pass the desired number of arguments at the time of construction. C++ Program to Use Initializer Lists in ConstructorsThe below example demonstrates how we can use the initializer list in the constructor in C++. C++ // C++ program to show how to use initializer list in // constructor #include <initializer_list> #include <iostream> #include <vector> using namespace std; // class declartion class MyClass { private: vector<int> vec; public: MyClass(initializer_list<int> initList) : vec(initList){} void display() { for (const auto& val : vec) { cout << val << " "; } cout << endl; } }; // driver code int main() { // passing initializer list MyClass obj({ 1, 2, 3, 4, 5 }); obj.display(); return 0; } Output1 2 3 4 5 Time Complexity: O(N), here N is the number of elements in the vec vector.Auxilliary Space: O(N) Explanation: In the above code, MyClass has a constructor that takes an std::initializer_list<int> that allows us to initialize MyClass with a list of integers, which are stored in a std::vector<int>. Comment More infoAdvertise with us Next Article Different Ways to Initialize an Set in C++ M maha123 Follow Improve Article Tags : C++ Programs C++ cpp-constructor C++-Constructors CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 min read How to Initialize Multimap with Default Values in C++? In C++, a multimap is a container provided by the STL library that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to initialize a multimap with default values in C++. Initialize Multi 2 min read Different Ways to Initialize an Set in C++ Initializing a set means assigning some initial values to the elements of the set container. In this article, we will learn different methods to initialize an std::set in C++.Table of ContentUsing Initializer ListOne by One InitializationFrom Another std::setFrom Another STL Container or ArrayUsing 3 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector 2 min read Like