A vector in C++ is a dynamic array provided by the Standard Template Library (STL) that can automatically resize itself when elements are added or removed. Initializing a vector means assigning initial values to its elements at the time of creation or after declaration.
- Store data efficiently in a dynamic container.
- Reduce manual memory management.
- Make code cleaner and easier to understand.
Different Ways to Initialize Vector
There are multiple ways to initialize a vector in C++, depending on how the data needs to be stored and managed.
Using Initializer List
We can initialize a vector with the list of values enclosed in curly braces {} known as initializer list. The values of the list will be assigned sequentially i.e. 1st value will be assigned to the 1st element of vector, 2nd value to 2nd element and so on.
Syntax
vector<type> v = {val1, val2, val3, ...};
Where:
type-> Data type of vector elementsval1, val2, val3...-> Initial values
// C++ Program to initializ std::vector with
// initializer list
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initializing std::vector with list of
// multiple values
vector<int> v = {11, 23, 45, 89};
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 23 45 89
One by One Initialization
Vector can be initialized by pushing value one by one. In this method, an empty vector is created, and elements are added to it one by one using the vector::push_back() method. This method is mostly used to initialize vector after declaration.
Syntax
v.push_back(val);
where, val is the value which we have to insert.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Pushing Value one by one
v.push_back(11);
v.push_back(23);
v.push_back(45);
v.push_back(89);
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 23 45 89
With a Single Value
We can initialize all the elements of the vector to a single value. We create a vector of a specified size and initialize all elements to the same value using vector constructor.
Syntax
vector<type> v(n, val);
Where:
- n -> Size of vector
- val -> Value assigned to all elements
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initializing all the elements of a
// vector using a single value
vector<int> v(5, 11);
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 11 11 11 11
From an Array
We can also initialize a vector using plain old static arrays using vector constructor. This works by copying all the elements of the array to the newly created vector.
Syntax
vector<type> v(arr, arr + n);
Where:
- arr -> Array name
- n -> Number of elements
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {11, 23, 45, 89};
int n = sizeof(arr) / sizeof(arr[0]);
// Initialize the std::vector v by arr
vector<int> v = {arr, arr + n};
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 23 45 89
From Another Vector
We can also initialize a newly created vector from an already created vector if they are of same type.
Syntax
vector<type> v2(v1.begin(), v1.end());
Where:
v1-> Existing vectorv2-> New vector
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1 = {11, 23, 45, 89};
// Initialize the vector v2 from vector v1
vector<int> v2(v1.begin(), v1.end());
for (auto i : v2)
cout << i << " ";
return 0;
}
Output
11 23 45 89
From Any STL Container
Vectors are flexible containers that can be initialized by any other already existing containers such as set, multiset, map, etc. if they are of same type.
Syntax
vector<type> v(first, last);
Where:
- first -> Iterator to first element
- last -> Iterator to element after the last element
Using std::fill() Function
We can also use the std::fill function to initialize the whole or a part of a vector to the same value.
Syntax
fill(first, last, val);
Where:
- first -> Starting iterator
- last -> Ending iterator
- val -> Value to assign
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v(5);
// Initialize vector v with 11
fill(v.begin(), v.end(), 11);
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 11 11 11 11
Using std::iota() Function
The std::iota() function from the <numeric> library allows us to initialize a vector with consecutive values starting from the given value.
Syntax
iota(first, last, val);
Where:
- first -> Starting iterator
- last -> Ending iterator
- val -> Starting value
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v(5);
// Using std::iota() to initialize vector v
// with 11
iota(v.begin(), v.end(), 11);
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 12 13 14 15