Instantiate vector c++: In the previous article, we have discussed about How to fill a vector with random numbers in C++. Let us learn how to Initialize a Vector in C++ Program.
5 Different Ways of Initialization of Vector
C++ init vector: In this article we will see 5 different ways to initialize a vector.
Without initializing, when we create vector object it creates an empty vector with no element.
std::vector<int> vecOfInts;
Let’s see one by one
- Initialization of vector with default value
- Initialization of a vector by filling the similar copy of an element
- Initialize a vector with an array
- Initialization of a vector with std::list
- Initialization of a vector with another vector
Method-1 : Initialization of vector with default value :
C++ initialize empty vector: In this example Vector is providing a constructor i.e. accepting the size as an argument and initializing the vector with that many objects of default values.
#include <bits/stdc++.h> using namespace std; int main() { // Initialize vector with 5 integers then Default value of all 5 ints will be 0. std::vector<int> vector1(3); cout << "vectors are :"; for (int i : vector1) cout << i << " " ; return 0; }
Output : vectors are :0 0 0
Method- 2 : Initialization of a vector by filling the similar copy of an element :
How to initialize a vector c++: When we want to initialize a vector with an element of particular value instead of default values, for that vector provides an overloaded constructor which accepts the size of vector and an element as an argument, Then initializes the vector with n elements of values.
#include <bits/stdc++.h> using namespace std; int main() { // Initialize vector with 5 integers then Default value of all 5 ints will be 0. std::vector<std::string>vector1(3, "hello !! "); cout << "vectors are :"; for (string s : vector1) cout << s << " "; return 0; }
Output : vectors are :hello !! hello !! hello !!
Method- 3 : Initialize a vector with an array :
How to initialize an empty vector in c++: When we want to initialize a vector with an array of elements, for doing that vector provides an over loaded constructor which accepts a range as an argument i.e. two iterators and initializes the vector with elements in range .
#include <bits/stdc++.h> using namespace std; int main() { std::string arr[] = { "1st", "2nd", "3rd", "4th" }; // Initialize vector with a string array std::vector<std::string> vector1(arr, arr + sizeof(arr) / sizeof(std::string)); cout << "vectors are :"; for (string s : vector1) cout << s << " "; return 0; }
Output : vectors are :1st 2nd 3rd 4th
Method- 4 : Initialization of a vector with std::list :
How to initialize empty vector c++: For this We will use the same overloaded constructor of vector to initialize a vector with range .
#include <bits/stdc++.h> using namespace std; int main() { // Create an std::list of 5 string objects std::list<std::string> lst; lst.push_back("1st"); lst.push_back("2nd"); lst.push_back("3rd"); lst.push_back("4th"); // Initialize a vector with std::list std::vector<std::string> vstr(lst.begin(), lst.end()); // Initialize a vector with other string object std::vector<std::string> vstr3(vstr); cout << "vectors are :"; for (string s : vstr3) cout << s <<" "; return 0; }
Output : vectors are :1st 2nd 3rd 4th
Method- 5 : Initialization of a vector with another vector :
Initialize vector c++: In this method Vector is providing a constructor i.e. receiving other vector as an argument and initializing the current vector with the copy of elements of provided vector .
#include <bits/stdc++.h> using namespace std; void demo() { vector<string> vstr; vstr.push_back("1st "); vstr.push_back("2nd "); vstr.push_back("3rd "); vector<string> vstr3(vstr); cout << "vectors are :"; for (string s : vstr3) cout << s <<" "; } int main() { demo(); return 0; }
Output : vectors are :1st 2nd 3rd