Passing a Vector to Constructor in C++



In C++, you can pass a std::vector to a class constructor to create a list of values when the object is created. So that the object can store or work with a list of values right from the beginning.

Why do You Pass a Vector to a Constructor?

  • Passing a Vector to a Constructor allows the object to be initialized with data at the time of creation.
  • When you pass a vector to a function, it simplifies the code by eliminating the need to initialize the function parameters, as the vector is already initialized during the function call.

Algorithm

Following is the algorithm of passing a vector to constructor in C++:

Begin
   Declare a class named as vector.
      Declare vec of vector type.
      Declare a constructor of vector class.
         Pass a vector object v as a parameter to the constructor.
         Initialize vec = v.
         Declare a function show() to display the values of vector.
            for (int i = 0; i < vec.size(); i++)
               print the all values of variable i.
   Declare v of vector type.
      Initialize some values into v in array pattern.
   Declare ob as an object against the vector class.
      Pass values of v vector via ob vector object to class vector.
   Call show() function using vector object to show the all values of
   vector v.
End.

Passing an Integer Vector to Constructor

When you pass an integer vector to a constructor, a list of integers is passed to the class during object creation. And, you can use these numbers when needed in the class.

Example

In this example, we create a class named Vector that takes a vector of integers as input using the constructor and display its elements using a show() function:

#include <iostream>
#include <vector>
using namespace std;
class Vector {
   vector<int> vec;
   public:
      Vector(vector<int> v) {
         vec = v;
}
void show() {
   for (int i = 0; i < vec.size(); i++)
      cout << vec[i] << " ";
   }
};
int main() {
   vector<int> v = {7,6,5,4};
   Vector ob(v);
   ob.show();
   return 0;
}

Following is the output to the above program:

7 6 5 4

Passing a Vector to Constructor using Initializer List

The initializer list provides a convenient way to initialize containers and user-defined types with a list of values. You can initialize a vector within a class constructor using an initializer list.

Example

In this example, we define a class Vector that stores a list of numbers which initializes using an initializer list and these numbers are stored using a function:

#include<iostream>
#include<vector>
using namespace std;
class Vector {
    vector<int>vec;
public:
   // initializer list
   Vector(const vector<int>& m):vec(m) {}
   void showVector() {
      for (int score : vec)
         cout<<score<<" ";
   }
};
int main() {
   vector<int>vec = {7,6,5,4};
   Vector s(vec);
   s.showVector();
   return 0;
}

Following is the output to the above program:

7 6 5 4 

Passing a Vector to Constructor by Reference

Passing a vector by reference to a class constructor means you no need to create any copy of the vector. Instead, the constructor can directly works with the original vector by reference.

Example

In this example, we are passing a vector by reference to a class by modifing it and adding a new value to display the updated vector.

#include<iostream>
#include<vector>
using namespace std;
class VectorRef {
   // Reference to the external vector
   vector<int>&refVec;
public:
   // Constructor taking a reference to a vector
   VectorRef(vector<int>& v):refVec(v) {}

   void modify() {
    // Modifies the original vector
       refVec.push_back(99);
   }
   void display() {
       for (int val : refVec)
           cout << val << " ";
   }
};
int main() {
   vector<int> nums = {7,6,5,4};
   // Pass the vector by reference to the object
   VectorRef obj(nums);
   // Modify the vector inside the class
   obj.modify();
   cout << "Vector after modification through class: ";
   obj.display(); 
   return 0;
}

Following is the output to the above program:

Vector after modification through class: 7 6 5 4 99 
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-05-26T17:24:41+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements