How to Initialize a Vector with Zero in C++? Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { // Initialize the vector with 0 vector<int> v(5, 0); for (auto i : v) cout << i << " "; return 0; } Output0 0 0 0 0 Note: If we do not provide any value in this method, then by default the vector will initialize with value 0.Apart from the above method, C++ also provides other different methods to initialize a vector with 0. Some of them are:Table of ContentOne by One InitializationUsing Vector assign()Using fill()One by One InitializationTo initialize the vector with value 0, we can insert the value 0 in the vector one by one using vector push_back() method. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Initialize the vector with 0 for (int i = 0; i < 5; i++) v.push_back(0); for (auto i : v) cout << i << " "; return 0; } Output0 0 0 0 0 Using Vector assign()The vector assign() method assigns the size of vector as well as it can also initialize the vector with some value like 0 in this case. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Initialize the vector with 0 v.assign(5, 0); for (auto i : v) cout << i << " "; return 0; } Output0 0 0 0 0 Using fill()The fill() method is used to fill the vector of given range with some specific value. So, to initialize the vector with value 0, we have to pass the specific value as 0. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Initialize the vector with 0 fill(v.begin(), v.end(), 0); for (auto i : v) cout << i << " "; return 0; } Output0 0 0 0 0 Comment More infoAdvertise with us Next Article How to Initialize a Vector with Zero in C++? A anmolpanxq Follow Improve Article Tags : C++ Programs C++ STL cpp-vector Practice Tags : CPPSTL Similar Reads 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 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 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 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 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 How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Resize a Vector Without Initializing New Elements in C++? In C++, when a vector is resized upward (increased size) using vector resize() method, the new elements are initialized to the default value for the vector's type (e.g., 0 for int). However, in some cases, initializing these new elements might be unnecessary in performance-critical applications.Vect 3 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 Reset int Array to Zero in C++? In C++, arrays store data of the same type in a contiguous memory location. To reset an int array means setting all elements to zero. In this article, we will see how to reset an integer array elements to zero in C++. For Example, Input: int arr[] = { 10, 20, 30, 40, 50 };Output:Array after resettin 2 min read Initialize a Vector with Hardcoded Elements 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 curl 2 min read Like