Open In App

Passing Vector to a Function in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

When we pass an array to a function, a pointer is actually passed.

However, to pass a vector there are two ways to do so:

  1. Pass By value
  2. Pass By Reference

When a vector is passed to a function, a copy of the vector is created. This new copy of the vector is then used in the function and thus, any changes made to the vector in the function do not affect the original vector.

For example, we can see below the program, changes made inside the function are not reflected outside because the function has a copy.

Example(Pass By Value):

CPP




// C++ program to demonstrate that when vectors
// are passed to functions without &, a copy is
// created.
#include <bits/stdc++.h>
using namespace std;
  
// The vect here is a copy of vect in main()
void func(vector<int> vect) { vect.push_back(30); }
  
int main()
{
    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);
  
    func(vect);
  
    // vect remains unchanged after function
    // call
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << " ";
  
    return 0;
}


Output

10 20 

Passing by value keeps the original vector unchanged and doesn’t modify the original values of the vector. However, the above style of passing might also take a lot of time in cases of large vectors. So, it is a good idea to pass by reference.

Example(Pass By Reference):

CPP




// C++ program to demonstrate how vectors
// can be passed by reference.
#include <bits/stdc++.h>
using namespace std;
  
// The vect is passed by reference and changes
// made here reflect in main()
void func(vector<int>& vect) { vect.push_back(30); }
  
int main()
{
    vector<int> vect;
    vect.push_back(10);
    vect.push_back(20);
  
    func(vect);
  
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << " ";
  
    return 0;
}


Output

10 20 30 

Passing by reference saves a lot of time and makes the implementation of the code faster.

Note: If we do not want a function to modify a vector, we can pass it as a const reference also.

CPP




// C++ program to demonstrate how vectors 
// can be passed by reference with modifications 
// restricted. 
#include<bits/stdc++.h> 
using namespace std; 
  
// The vect is passed by constant reference 
// and cannot be changed by this function. 
void func(const vector<int> &vect) 
    // vect.push_back(30);   // Uncommenting this line would 
                            // below error 
    // "prog.cpp: In function 'void func(const std::vector<int>&)': 
    // prog.cpp:9:18: error: passing 'const std::vector<int>' 
    // as 'this' argument discards qualifiers [-fpermissive]" 
      
    for (int i = 0; i < vect.size(); i++) 
    cout << vect[i] << " "
  
int main() 
    vector<int> vect; 
    vect.push_back(10); 
    vect.push_back(20); 
  
    func(vect); 
      
    return 0; 
}


Output

10 20 

 



Last Updated : 27 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads