How to Initialize a Vector with Default Values in C++? Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { // Initialze vector with default value 9 vector<int> v(5, 9); for (auto i : v) cout << i << " "; return 0; } Output9 9 9 9 9 There are also some other methods in C++ to initialize the vector with default value. Some of them are as follows:Table of ContentUsing vector assign()Using fill()Using iota()Using Vector assign()The vector assign() method can also be used to assign the existing vector with specified size and some default value. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Initialze vector with default value 9 v.assign(5, 9); for (auto i : v) cout << i << " "; return 0; } Output9 9 9 9 9 Using fill()In this method, first we create the vector with specified size and then we fill the vector with default value using fill() method. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Initialze vector with default value 9 fill(v.begin(), v.end(), 9); for (auto i : v) cout << i << " "; return 0; } Output9 9 9 9 9 Note: With this method, we can initialize the range of vector with default value.Using iota()If we want to initialize our vector with sequential default value, then simply we can use iota() method. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Initialze vector with sequential default value // starting with 9 iota(v.begin(), v.end(), 1); for (auto i : v) cout << i << " "; return 0; } Output1 2 3 4 5 Comment More infoAdvertise with us Next Article How to Initialize a Vector with Default Values in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 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 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 generate a vector with random values in C++? Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.The recommended method to initialize a vector with random values is by using 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