Initialize a Vector with Hardcoded Elements Last Updated : 05 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report C++ allows us to initialize the vector with hardcoded (predefined) elements. In this article, we will learn how to initialize the vector with predefined elements.The simplest method to initialize the vector with hardcoded elements is to use an initializer list where we specify the values inside curly braces {}. C++ #include <bits/stdc++.h> using namespace std; int main() { // Initialize vector with hardcoded elements vector<int> v = {1, 3, 7, 9}; for (auto i : v) cout << i << " "; return 0; } Output1 3 7 9 Explanation: Here, the initializer list {1, 3, 7, 9} defines the elements of the vector. This method is concise and efficient, especially for initializing small vectors.Apart from above method, vector assign() can also be used to initialize the vector with predefined values. In this method we have to pass the hardcoded elements as an initializer list inside the assign() function. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Initialize vector with hardcoded elements v.assign({1, 3, 7, 9}); for (auto i : v) cout << i << " "; return 0; } Output1 3 7 9 Explanation: In this method, the assign() function populate the vector with values written in the program. This is useful when reinitializing an already existing vector. Comment More infoAdvertise with us Next Article Initialize a Vector with Hardcoded Elements A anmolpanxq Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim 2 min read How to Initialize a Vector with Values between a Range in C++? In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector 2 min read How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read How to Initialize a Deque from a Vector in C++? In C++, the Standard Template Library(STL) provides a container deque also known as a double-ended queue where insertion and deletions are possible from both ends. On the other hand, vectors are dynamic containers that can resize themselves during the insertion or deletion of elements. In this artic 2 min read Like