Table of contents
1.
Introduction
2.
What is a Vector in C++?
3.
Vector Initialization methods in C++
4.
1. Initializing vector by passing an array to the constructor of the vector class
4.1.
C++
5.
2. Initializing the vector by inserting the values one-by-one
5.1.
C++
6.
3. Initializing a vector by using an existing array
6.1.
C++
7.
4. Initialize a vector by using another existing vector
7.1.
C++
8.
5. Initializing the vector by using the overloaded constructor
8.1.
C++
9.
Frequently Asked Questions
9.1.
How to initialize a vector in C++ with new keyword?
9.2.
How do you initialize a vector with default values in C++?
9.3.
How to initialize a vector to null in C++?
9.4.
Why do we need initialization vector?
9.5.
How is vector created in C++?
10.
Conclusion
Last Updated: Oct 8, 2024
Easy

Different ways to initialize a vector in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++, vectors are dynamic arrays that can grow or shrink in size as needed. Unlike regular arrays, vectors can handle memory management automatically. Vector initialization allows programmers to create and populate vectors in different ways, making it easier to store and manage data. There are various methods to initialize vectors in C++, each offering flexibility based on the specific needs of the program. 

Vector Initialization in C++

Let us learn more about vectors and the different ways in which we can initialize them.

What is a Vector in C++?

Vectors in C++ are a part of the C++ standard template library. Vectors can be understood as a dynamic array that can change their size whenever an element is added or deleted. It can store elements of the same data types. The elements of a vector are stored in contiguous memory locations and can be accessed by using an iterator.

To use a vector, we have to include the vector header file:

#include <vector>

 

To declare a vector, we use:

Vector <Data_type> vector_name;

 

For example:

Vector <int> vect;

 

Here, vect is the name of the vector of integer data type.

Vector Initialization methods in C++

Let us now look at some of the common methods to initialize a vector in C++.

  1. Initializing vector by passing an array to the constructor of the vector class
     
  2. Initialize the vector by inserting the values one-by-one
     
  3. Initialize a vector by using an existing array
     
  4. Initialize a vector by using another existing vector
     
  5. Initializing the vector by using the overloaded constructor

1. Initializing vector by passing an array to the constructor of the vector class

We can initialize a vector by passing an array to the constructor of the vector class. It is similar to initializing an array by providing all the values to the vector in one go.

Code:

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
//initializing the vector like an array
vector<char> vec
{
'c', 'o', 'd', 'i', 'n', 'g', ' ', 'n', 'i', 'n', 'j', 'a', 's'
};
// printing out the vector elements
for (char x: vec)
cout << x;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

coding ninjas

2. Initializing the vector by inserting the values one-by-one

This method first declares a vector and then uses push_back to insert the values one-by-one into the vector.

Code:

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
   //creating an empty character vector
vector<char> vec;

//inserting the values one by one
vec.push_back('c');
vec.push_back('o');
vec.push_back('d');
vec.push_back('i');
vec.push_back('n');
vec.push_back('g');
vec.push_back(' ');
vec.push_back('n');
vec.push_back('i');
vec.push_back('n');
vec.push_back('j');
vec.push_back('a');
vec.push_back('s');

//printing out the vector elements
for (char x : vec)
cout << x;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

coding ninjas

3. Initializing a vector by using an existing array

We can also initialize a vector by passing an existing array to the iterator constructor of the vector class. All the elements of the array will fill the vector in the same order. The size of the vector will change accordingly.

Code:

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
// initializing the array
char arr[] = { 'c', 'o', 'd', 'i', 'n', 'g', '  ', 'n', 'i', 'n', 'j', 'a', 's'
};
int n = sizeof(arr) / sizeof(arr[0]);

// initializing the vector
vector<char> vect(arr, arr + n);

// printing out the vector elements
for (char i: vect)
cout << i;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

coding ninjas

4. Initialize a vector by using another existing vector

We use a range constructor to initialize a vector by using an existing vector. We pass the initial and final position of the iterator. The elements in the iterator range in the existing vector will be inserted into the new vector.

Code:

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
//initializing the first vector
vector<char> vec1
{
'c', 'o', 'd', 'i', 'n', 'g', ' ', 'n', 'i', 'n', 'j', 'a', 's'
};

// initializing the vector by using range constructor
vector<char> vec2(vec1.begin(), vec1.end());

for (char i: vec2)
cout << i;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

coding ninjas

5. Initializing the vector by using the overloaded constructor

An overloaded constructor can also be used to initialize a vector. It accepts two parameters, the size of the vector and the value that needs to be inserted into the vector. The overloaded constructor inserts the same value into the vector multiple times. The number of times is equal to the size given.

Code:

  • C++

C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
// specifying the size 
int size = 10;

// initializing vector using overloaded constructor 
vector<int> vec(size, 10);

// printing out the vector elements
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

10 10 10 10 10 10 10 10 10 10 

Frequently Asked Questions

How to initialize a vector in C++ with new keyword?

In C++, you can initialize a vector with the new keyword by dynamically allocating memory using std::vector<int>* vec = new std::vector<int>();. This creates a pointer to the vector on the heap.

How do you initialize a vector with default values in C++?

You can initialize a vector with default values using std::vector<int> vec(n, value);, where n is the size and value is the default value assigned to all elements.

How to initialize a vector to null in C++?

In C++, vectors cannot be explicitly initialized to null. However, you can create an empty vector using std::vector<int> vec;, which starts with no elements.

Why do we need initialization vector?

Initialization of vectors is important for defining their size and default values before using them, ensuring proper memory allocation and avoiding undefined behavior when accessing elements.

How is vector created in C++?

In C++, a vector is created using the Standard Template Library (STL). You can declare a vector by including the <vector> header and initializing it with syntax like std::vector<int> myVector;, specifying the data type and optional size.

Conclusion

In this blog, we introduced vectors and explored various methods to initialize them. We discussed five different ways to initialize a vector based on your requirements. For more insights on vectors, be sure to check out our detailed blog on Vectors in C++.

 

Live masterclass