Initialize a Vector with Hardcoded Elements Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 info A anmolpanxq Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++7 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++10 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like