Last Updated : 01 Jul, 2024
Summarize
Comments
Improve
Vectors in C++, like Arrays, are one of the most extensively used entities and to initialize vectors in C++ is one of the most common issues that users face. One of the most commonly used methods for vector initialization is the Array style. But C++, along with this, provides several different methods to initialize a vector. In this post, we have looked into such 7 different ways of how you can initialize Vectors in C++.
Table of Content
- 1. Initializing Vector by Pushing values One by One
- 2. Initializing Vector by Specifying Size and Initializing All Values
- 3. Initializing Vector like Arrays
- 4. Initializing Vector from an Array
- 5. Initializing Vector from Another Vector
- 6. Initializing all Elements of Vector with a Particular Value
- 7. Initialize Vector with Consecutive Numbers using std::iota
Let us look into them one by one:
1. Initializing Vector by Pushing values One by One
Vector can be initialized by pushing value one by one. This method involves creating an empty vector and adding elements to it one by one using the push_back()
function.
Syntax
vector_name.push_back(value)
Example
// C++ program to create an empty// vector and push values one// by one.#include <iostream>#include <vector>using namespace std;int main(){ // Create an empty vector vector<int> vect; vect.push_back(10); vect.push_back(20); vect.push_back(30); for (int x : vect) cout << x << " "; return 0;}
Output
10 20 30
Time Complexity: O(n), where n is the number of elements being pushed.
Auxiliary Space: O(n), where n is the number of elements being stored.
2. Initializing Vector by Specifying Size and Initializing All Values
In this method, we create a vector of a specified size and initialize all elements to the same value. We do it by passing the size and the default value to the vector constructor in vector declaration.
Syntax
vector<type> vector_name(size, default_value);
Example
// C++ program to create an empty // vector and push values one// by one.#include <iostream>#include <vector>using namespace std;int main(){ int n = 3; // Create a vector of size n with // all values as 10. vector<int> vect(n, 10); for (int x : vect) cout << x << " "; return 0;}
Output
10 10 10
Time Complexity: O(n), where n is the size of the vector.
Auxiliary Space: O(n), where n is the size of the vector.
3. Initializing Vector like Arrays
We can initialize a vector with a list of values similar to how you would initialize an array. This can be by passing assigning the list of values to the vector during vector declaration.
Syntax
vector<type> vector_name = {v1, v2, v3 ....};
Example
// C++ program to initialize // a vector like an array.#include <iostream>#include <vector>using namespace std;int main(){ vector<int> vect{ 10, 20, 30 }; for (int x : vect) cout << x << " "; return 0;}
Output
10 20 30
Time Complexity: O(n), where n is the number of elements being initialized.
Auxiliary Space: O(n), where n is the number of elements being stored.
4. Initializing Vector from an Array
A vector can be initialized using an existing array. We just need to pass the pointer to the first element of the array and the hypothetical element after the last element of the array.
Syntax
vector<type> vector_name(arr, arr + size);
Example
// C++ program to initialize// a vector from an array.#include <iostream>#include <vector>using namespace std;int main(){ int arr[] = { 10, 20, 30 }; int n = sizeof(arr) / sizeof(arr[0]); vector<int> vect(arr, arr + n); for (int x : vect) cout << x << " "; return 0;}
Output
10 20 30
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(n), where n is the number of elements being stored.
5. Initializing Vector from Another Vector
Like previous method, we can create a new vector by copying elements from an existing vector using iterators. We pass the begin() and end() iterator of the another vector to the vector constructor.
Syntax
vector<type> vector_name(vec1.begin(), vec1.end());
Example
// C++ program to initialize a vector from// another vector.#include <iostream>#include <vector>using namespace std;int main(){ vector<int> vect1{ 10, 20, 30 }; vector<int> vect2(vect1.begin(), vect1.end()); for (int x : vect2) cout << x << " "; return 0;}
Output
10 20 30
Time Complexity: O(n), where n is the number of elements in the source vector.
Auxiliary Space: O(n), where n is the number of elements being stored
6. Initializing all Elements of Vector with a Particular Value
We can also use the std::fill
function to initialize all elements of a vector to the same value. This method is similar to the method 2 but we need to pass the begin and end iterator of the vector to the fill method along with the value to be filled.
Syntax
fill(vector_name.begin(), vector_name.end(), value);
Example
// C++ Program to initialize vector using fill()#include <iostream>#include <vector>using namespace std;int main(){ // creating array with size 10 vector<int> vect1(10); // initializing using fill() function int value = 5; fill(vect1.begin(), vect1.end(), value); // printing vector for (int x : vect1) cout << x << " "; return 0;}
Output
5 5 5 5 5 5 5 5 5 5
Time Complexity: O(n), where n is the size of the vector.
Auxiliary Space: O(1) additional space for the value variable.
7. Initialize Vector with Consecutive Numbers using std::iota
The iota function from the <numeric> library
allows you to initialize a vector with consecutive values efficiently. We need to pass the begin and end iterator with the value of the first element to the iota() function.
Syntax
iota(vector_name.begin(), vector_name.end(), value);
Example
// C++ program to initialize a // vector with consecutive// numbers#include <iostream>#include <numeric>#include <vector>using namespace std;int main(){ // declaring a vector with size 5 vector<int> vec(5); // initializing using iota() iota(vec.begin(), vec.end(), 1); // printing the vector for (int i = 0; i < 5; i++) { cout << vec[i] << " "; } return 0;}
Output
1 2 3 4 5
Time Complexity: O(n), where n is the size of the vector.
Auxiliary Space: O(1) additional space for the iota
function.
Previous Article
Vector in C++ STL
Next Article
vector::begin() and vector::end() in C++ STL