Ways to Initialize a Vector in C++
The following different ways can be used to initialize vector in C++:
Using the push_back() Method to Push Values Into the Vector
The push_back() method of the class vector is used to push or insert elements into a vector. It inserts each new element at the end of the vector and the vector size is increased by 1.
Syntax
vector_name.push_back(data)
vector_name is the name of the vector. The push_back() method accepts one element and inserts it at the end of the vector.
Example
Consider a vector v1.
Input: v1.push_back(100)
v1.push_back(200)
v1.push_back(300)
Output: Resulting vector is:
100 200 300 with size 3.
Input: v1.push_back(1)
v1.push_back(2)
Output: Resulting vector is:
100 200 300 1 2 with size 5.
The following program illustrates how to initialize a vector in C++ using the push_back() method:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// declare a vector
vector<int> v1;
// initialize vector using push_back()
v1.push_back(100);
v1.push_back(200);
v1.push_back(300);
v1.push_back(1);
v1.push_back(2);
cout << "The elements in the vector are:\n";
// traverse vector
for (int i = 0; i < v1.size(); i++)
{
// print the vector elements
cout << v1[i] << " ";
}
return 0;
}
Full Stack Web Developer Course
To become an expert in MEAN StackView Course
Steps
- Declare a vector v1.
- Initialize the vector using the push_back() method. Insert vector elements one by one
- Traverse the vector
- Print the vector elements
Using the Overloaded Constructor
The overloaded constructor can also be used to initialize vectors in C++. This constructor accepts two parameters. The size of the vector and the value to be inserted is provided while initializing the vector. The value provided will be inserted into the vector multiple times (equal to the provided size of the vector).
Syntax
vector<type> vector_name(size, data)
The parameter size is the number of elements to be inserted into the vector (or simply the size of the vector) and the data is the value to be inserted.
Also Read: Constructor in C++: A Comprehensive Guide to Constructor
Example
Input: vector<int> v1(5, 2)
Output: The resulting vector will be:
2 2 2 2 2
Input: vector<int> v2(4, 1)
Output: The resulting vector will be:
1 1 1 1
The following program illustrates how to initialize a vector in C++ using the overloaded constructor:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// initialize size
int size = 5;
// initialize vector using overloaded constructor
vector<int> v1(size, 2);
// print the vector elements
cout << "The vector v1 is: \n";
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << "\n";
// initialize vector v2
vector<int> v2(4, 1);
// print elements of vector v2
cout << "\nThe vector v2 is: \n";
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << "\n\n";
return 0;
}
New Course: Full Stack Development for Beginners
Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
Steps
- Initialize the size of the vector
- Initialize the vector using the overloaded constructor by specifying the size and the value to be inserted
- Traverse the vector
- Print the vector elements
Passing an Array to the Vector Constructor
Another way to initialize a vector in C++ is to pass an array of elements to the vector class constructor. The array elements will be inserted into the vector in the same order, and the size of the vector will be adjusted automatically.
Syntax
vector<type> vector_name{array of elements}
You pass the array of elements to the vector at the time of initialization.
Example
Input: vector<int> v1{10, 20, 30, 40, 50}
Output: The resulting vector will be:
10 20 30 40 50
Input: vector<char> v2{'a', 'b', 'c', 'd', 'e'}
Output: The resulting vector will be:
a b c d e
The following program illustrates how to initialize a vector in C++ by passing an array to the vector class constructor:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// pass an array of integers to initialize the vector
vector<int> v1{10, 20, 30, 40, 50};
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << "\n\n";
// pass an array of characters to initialize the vector
vector<char> v2{'a', 'b', 'c', 'd', 'e'};
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << "\n\n";
return 0;
}
Full Stack Java Developer Course
In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Steps
- Declare a vector
- Initialize the vector. Pass an array of elements to the vector class constructor
- Traverse the vector
- Print the vector elements
Using an Existing Array
This is one of the most standard ways to initialize vectors in C++ elegantly. You can initialize a vector by using an array that has been already defined. You need to pass the elements of the array to the iterator constructor of the vector class.
Also Read: Iterators in C++: An Ultimate Guide to Iterators
Syntax
data_type array_name[n] = {1,2,3};
vector<data_type> vector_name(arr, arr + n)
The array of size n is passed to the iterator constructor of the vector class.
Example
Input: int a1[5] = {10, 20, 30, 40, 50}
vector<int> v1(a1, a1 + 5)
Output: The resulting vector will be:
10 20 30 40 50
Input: char a2[5] = {'a', 'b', 'c', 'd', 'e'}
vector<int> v2(a2, a2 + 5)
Output: The resulting vector will be:
a b c d e
The following program illustrates how to pass an existing array to the iterator constructor of the vector class to initialize a vector in C++:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// initialize an array of integers
int a1[5] = {10, 20, 30, 40, 50};
// initialize vector
vector<int> v1(a1, a1 + 5);
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << "\n\n";
// initialize an array of char
char a2[5] = {'a', 'b', 'c', 'd', 'e'};
vector<int> v2(a2, a2 + 5);
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << "\n\n";
return 0;
}
Caltech Coding Bootcamp
Become a full stack developer in 6 monthsEnroll Now
Steps
- Initialize an array
- Pass the array to the iterator constructor of the vector class to initialize the vector
- Traverse the vector
- Print the vector elements
Using an Existing Vector
The range constructor of the vector class can also be used to initialize a vector. You pass the iterators of an existing vector to the constructor to specify the range of the elements to be inserted into the new vector.
Syntax
vector<type> vector_name(iterator_first, iterator_last)
Here, you must pass the iterators pointing to the existing vector to the constructor, to initialize the new vector. The elements lying in the range that is passed as arguments are inserted into the new vector.
Example
Input: vector<int> v1{10, 20, 30, 40, 50};
vector<int> v2(v1.begin(), v1.end())
Output: The resulting vector will be:
10 20 30 40 50
Input: vector<int> v1{10, 20, 30, 40, 50};
vector<int> v2(v1.begin(), v1.begin() + 3)
Output: The resulting vector will be:
10 20 30
The following program illustrates how to initialize a vector in C++ using an existing vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// initialize a vector
vector<int> v1{10, 20, 30, 40, 50};
// initialize vectors using the range constructor
vector<int> v2(v1.begin(), v1.end());
vector<int> v3(v1.begin(), v1.begin() + 3);
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << "\n\n";
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v3.size(); i++)
{
cout << v3[i] << " ";
}
cout << "\n\n";
return 0;
}
Steps
- Initialize a vector using any of the methods discussed above
- Pass the iterators of this vector (specifying the range) to the range constructor of the new vector
- Traverse the vector
- Print the vector elements
Using the fill() Method
The fill() function, as the name suggests, fills or assigns all the elements in the specified range to the specified value. This method can also be used to initialize vectors in C++.
Syntax
fill(begin, end, value)
The fill() function accepts three parameters begin and end iterators of the range and the value to be filled. The iterator begin is included in the range whereas the iterator end is not included.
Example
Consider vectors v1 and v2 of size 5.
Input: fill(v1.begin(), v1.end(), 6)
Output: The resulting vector will be:
6 6 6 6 6
Input: fill(v2.begin() + 2, v2.begin() + 4, 6)
Output: The resulting vector will be:
0 0 6 6 0
The following program illustrates how to initialize a vector in C++ using the fill() function:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// declare a vector
vector<int> v1(5);
// initialize the vector using the fill() method
fill(v1.begin(), v1.end(), 6);
// declare another vector
vector<int> v2(5);
// initialize v2
fill(v2.begin() + 2, v2.begin() + 4, 6);
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << "\n\n";
// print vector elements
cout << "The vector elements are: \n";
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << "\n\n";
return 0;
}
Steps
- Declare a vector
- Pass the range and the value to the fill() function to initialize the vector
- Traverse the vector
- Print the vector elements
Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!