Last Updated : 11 Nov, 2024
To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.
C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.
Pass Vector by ValueThe pass by value method is the simplest method to pass any vector to the function. This method will create a copy of the original vector for passing. Therefore, modifications done inside the function will not affect the original vector.
SyntaxIn the function declaration, we define the argument name and type as shown:
rtype foo(vector<type> para_name);
In the function call, we just pass the vector name.
foo(vec_name);
where,
#include <bits/stdc++.h>
using namespace std;
// Pass by value
void foo(vector<int> vec) {
// Modifying vector inside function
vec.push_back(8);
for (auto i: vec) {
cout << i << " ";
}
cout << endl;
}
int main() {
vector<int> v = {1, 3, 7, 9};
// Passing the vector to foo()
foo(v);
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: The vector inside the function got one more element while the original vector remained unchanged even after the function execution.
This method is only preferred if we don't want to modify the vector, and the vector size is small because making a copy of a large vector is expensive
Pass Vector by ReferenceThe pass by reference method passes the reference of the vector to the function. It does not create the copy of vector therefore, all the modifications done in the function will be reflected in the original array.
SyntaxIn the function declaration,
rtype foo(vector<type>& para_name);
The &
symbol indicates that the vector is passed by reference.
In the function call, we just pass the vector name.
Example C++foo(vec_name);
#include <bits/stdc++.h>
using namespace std;
// Pass by reference
void foo(vector<int> &vec) {
// Modifying the vector
vec.push_back(8);
for (auto i : vec)
cout << i << " ";
cout << endl;
}
int main() {
vector<int> v = {1, 3, 7, 9};
// Pass the vector to foo()
foo(v);
for (auto i : v)
cout << i << " ";
return 0;
}
1 3 7 9 8 1 3 7 9 8
Explanation: The function modified the original vector as we can see in the output.
Constant VectorIf we want to pass the vector to a function without modifying it, we can pass it as a constant reference using const
. This avoids unnecessary copying while ensuring the original vector remains unchanged.
The pass by pointer method is similar to the pass by reference method, but instead of a reference, we pass the memory address of the vector to the function.
SyntaxIn the function declaration,
rtype foo(vector<type>* para_name);
The *
symbol indicates that the pointer to the vector.
In the function call, we just pass address of the vector.
Example C++foo(vec_name);
#include <bits/stdc++.h>
using namespace std;
// Pass by pointer
void foo(vector<int> *ptr) {
// Modify the original vector
ptr->push_back(8);
for (int i = 0; i < ptr->size(); i++) {
cout << ptr->at(i) << " ";
}
cout << endl;
}
int main() {
vector<int> v = {1, 3, 7, 9};
// Passing address of the vector to foo()
foo(&v);
for (auto i : v)
cout << i << " ";
return 0;
}
1 3 7 9 8 1 3 7 9 8
Explanation: The function modified the original vector as we can see in the output.
This method is not recommended as it brings the errors and risks associated with pointers.
Pass by Value Vs Pass by Reference Vs Pass by PointerFollowing are the differences between the pass by value, pass by reference and pass by pointer method:
Pass by Value
Pass by Reference
Pass by Pointer
This method creates a copy of vector which is passed to the function.
This method passes the reference to the original vector and does not create copy.
This method passes the address of original vector and does not create the copy of vector.
Requires extra memory to store the copy.
No additional memory is needed.
No additional memory is needed.
Original vector remains unchanged.
Changes will reflect in original vector.
Changes will reflect in original vector.
Slower as compared to other methods
Fast
Fast
No special operator is need in function declaration and call.
& operator in the function declaration specifies that the reference is being passed is a reference.
* operator in the function declaration and & operator to get the address of the vector is used in pass by pointers.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4