Passing vector to a function in C++

C++ STL | Passing vector to a function: In this article, we are going to see how to pass a vector as a parameter in functions in C++ with examples?
Submitted by Radib Kar, on July 09, 2020

Passing an argument can be of two types generally:

  1. Pass by value
  2. Pass by reference

In the case of passing a vector as a parameter in any function of C++, the things are not different. We can pass a vector either by value or by reference.

In the following section, we will discuss when we should use what kind of passing and what's difference b/w them.

1) Passing a vector by value

Below is an example of passing the vector by value. In the below example, we have tried to figure out that if we pass a vector by value, whether local changes in the function reflects the original vector in main or not.

Let's see the example first, then we will detail what happened and why.

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> arr)
{ //paseed by value

    //printing the vector which has been passed
    cout << "In the print function...\n";

    cout << "Printing the array that has been passed by value\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;

    cout << "changes made in array...\n";
    for (int i = 0; i < arr.size(); i++)
        arr[i] = i + arr[i];

    cout << "printing after making the changes\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
}

int main()
{
    //initialize the vector with size 5 and element 1
    vector<int> a(5, 1);

    //to print we will use function
    print(a);

    //again print here to check
    //whether the change made in print is 
    //reflecting here or not
    cout << "printing in main\n";
    for (auto it : a) {
        cout << it << " ";
    }
    cout << endl;

    cout << "change not reflected\n";

    return 0;
}

Output:

In the print function...
Printing the array that has been passed by value
1 1 1 1 1
changes made in array...
printing after making the changes
1 2 3 4 5
printing in main
1 1 1 1 1
change not reflected

So, we found that the changes we made in the print function is not reflected in the main. It's similar to other datatypes for the call by value.

What happens when we pass the vector by value?

When we pass the vector into the function by value then it simply makes a deep copy of the vector to a new location and the function operates on the new copy of the vector. That's why the changes made are not reflected back to the main vector as it's a deep copy.

Also, since it copies the vector, thus it's time-consuming.

When we should pass by value?

When we need to ensure that the function that operates on the vector argument makes changes but that shouldn't interfere with the original vector then we must use pass by value. To details when we need to keep our original vector unchanged throughout the function execution we can pass by value. The vector remains unmodified till the end.

2) Pass a vector by reference

Below is an example of passing the vector by reference. In the below example, we have tried to figure out that if we pass a vector by reference, whether local changes in the function reflects back to the original vector in main or not.

Let's see the example first, then we will detail what happened and why.

#include <bits/stdc++.h>
using namespace std;

void print(vector<int>& arr)
{ //passed by reference

    //printing the vector which has been passed
    cout << "In the print function...\n";

    cout << "Printing the array that has been passed by reference\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;

    cout << "changes made in array...\n";
    for (int i = 0; i < arr.size(); i++)
        arr[i] = i + arr[i];

    cout << "printing after making the changes\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
}

int main()
{
    //initialize the vector with size 5 and element 1
    vector<int> a(5, 1);

    //to print we will use function
    print(a);

    //again print here to check
    //whether the change made in print is 
    //reflecting here or not
    cout << "printing in main\n";
    for (auto it : a) {
        cout << it << " ";
    }
    cout << endl;

    cout << "change is reflected\n";

    return 0;
}

Output:

In the print function...
Printing the array that has been passed by reference
1 1 1 1 1
changes made in array...
printing after making the changes
1 2 3 4 5
printing in main
1 2 3 4 5
change is reflected

So we found that the changes we made in the print function is reflected in the main. It's similar to other datatypes for the call by reference.

What happens when we pass the vector by reference?

When we pass the vector into the function by reference then it doesn't make any new copy of the vector, rather the function operates on the same the vector, same memory location.

That is what passing reference means. It simply means the same copy is passed. That's why the changes made in the printf function is reflected back to the main vector.

Also, since it doesn't make any new copy it's faster.

When we should pass by reference?
When we need the vector to be modified by the function, we have to pass the vector by reference. Otherwise, changes made in the function won't be reflected.

Below is the detailed use cases about when we should pass the vector by value and when we should pass the vector by reference.

  1. Vector needs to be modified via function: pass by reference
  2. Vector can't be modified via function, but there is some modification operation in the function: pass by value
  3. Function not doing any kind of modification: either pass by value or pass by reference. But pass by reference is a better option since it's faster.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.