Open In App

Passing a Vector to Constructor in C++

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Just like any other data, we can also pass a vector to a constructor of the desired class. We can pass it by value or by reference. What we do with it depends on our requirement. The following are some of the common cases:

Copy Data to Member Vector

If we want to store the copy of the vector in the class, we can just assign the passed vector to the member vector of the class in the constructor body.

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

class MyClass {
    vector<int> vec;

public:

    // Assigned passed vector to member vector
    MyClass(vector<int> v) {
       vec = v;
    }
    
    void print() {
        for (int i = 0; i < vec.size(); i++)
            cout << vec[i] << " ";
    }
};

int main() {
    vector<int> vec;
    for (int i = 1; i <= 5; i++)
        vec.push_back(i);
        
    // Passing vector to MyClass's constructor
    MyClass obj(vec);
    obj.print();
    return 0;
}

Output
1 2 3 4 5 

Time Complexity: O(n)
Space Complexity: O(n)

We can also initialize using the initializer list.

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

class MyClass {
    vector<int> vec;

public:

    // Initializing using initializer list
    MyClass(vector<int> v) : vec(v) {}
    
    void print() {
        for (int i = 0; i < vec.size(); i++)
            cout << vec[i] << " ";
    }
};

int main() {
    vector<int> vec;
    for (int i = 1; i <= 5; i++)
        vec.push_back(i);

    // Passing vector to MyClass's constructor
    MyClass obj(vec);
    obj.print();
    return 0;
}

Output
1 2 3 4 5 

Time Complexity: O(n), to create copy of vector.
Space Complexity: O(n)

Create a Reference to the Vector

In C++, references must be initialized when they are declared. It holds true for reference members of the class. So, we have to use initializer list in the constructor to assign the vector reference member to the passed vector.

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

class MyClass {
    vector<int>& vec;
public:

    // This is the right way to assign
    // the reference of stl container
    MyClass(vector<int>& arr)
        : vec(arr) {}

    void print() {
        for (int i = 0; i < vec.size(); i++)
            cout << vec[i] << " ";
    }
};

int main() {
    vector<int> vec;
    for (int i = 1; i <= 5; i++)
        vec.push_back(i);
        
    // Passing vector to MyClass's constructor
    MyClass obj(vec);
    obj.print();
    return 0;
}

Output
1 2 3 4 5 

Time Complexity: O(1)
Space Complexity: O(1)

Pass the Ownership of the Vector

If the ownership of the vector passed to the constructor is to be given to the class, then we have to use the move semantics.

C++
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

class MyClass {
    vector<int> vec;
public:

    // This is the right way to assign
    // the reference of stl container
    MyClass(vector<int>&& arr)
        : vec(move(arr)) {}

    void print() {
        for (int i = 0; i < vec.size(); i++)
            cout << vec[i] << " ";
    }
};

int main() {
    vector<int> vec;
    for (int i = 1; i <= 5; i++)
        vec.push_back(i);
        
    // Passing vector to MyClass's constructor
    MyClass obj(move(vec));
    obj.print();
    return 0;
}

Output
1 2 3 4 5 

Time Complexity: O(1)
Space Complexity: O(1)


Next Article
Practice Tags :

Similar Reads