Pass Vector by Reference in C++

  1. Use the vector<T> &arr Notation to Pass a Vector by Reference in C++
  2. Use the const vector<T> &arr Notation to Pass a Vector by Reference in C++

This article will demonstrate multiple methods about how to pass a vector by reference in C++.

Use the vector<T> &arr Notation to Pass a Vector by Reference in C++

std::vector is a common way to store arrays in C++, as they provide a dynamic object with multiple built-in functions for manipulating the stored elements. Note that vector can have quite a large memory footprint, so one should carefully consider when passing them to functions. Usually, it’s the best practice to pass by reference and evade the whole object’s copy in for the function scope.

In the following example, we demonstrate a function that takes a single vector of ints by reference and modifies its elements. vector elements are printed before and after the multiplyByTwo call in the main function. Note that, even though we store the return value in a new variable arr_mult_by2, we could access it with the original arr name since the elements were modified in the same object and no new copy was returned.

#include <iostream>
#include <vector>
#include <iterator>

using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::string;

vector<int> &multiplyByTwo(vector<int> &arr){
    for (auto &i : arr) {
        i *= 2;
    }
    return arr;
}

int main() {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    cout << "arr          - ";
    copy(arr.begin(), arr.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    auto arr_mult_by2 = multiplyByTwo(arr);

    cout << "arr_mult_by2 - ";
    copy(arr_mult_by2.begin(), arr_mult_by2.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

arr          - 1; 2; 3; 4; 5; 6; 7; 8; 9; 10;
arr_mult_by2 - 2; 4; 6; 8; 10; 12; 14; 16; 18; 20;

Use the const vector<T> &arr Notation to Pass a Vector by Reference in C++

On the other hand, one can guarantee that the passed reference would be accessible for modification in the function definition. This feature is provided with the const qualifier keyword that tells the compiler to prohibit any modifications to the given object in the current function scope. Note that this may seem optional detail that does not need to be stressed on a developer, but sometimes these keywords may help the compiler to optimize machine code for better performance.

#include <iostream>
#include <vector>
#include <iterator>

using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::string;

vector<int> &multiplyByTwo(vector<int> &arr){
    for (auto &i : arr) {
        i *= 2;
    }
    return arr;
}

void findInteger(const vector<int> &arr) {
    int integer = 10;
    for (auto &i : arr) {
        if (i == integer) {
            cout << "found - " << integer << " in the array" << endl;
            return;
        }
    }
    cout << "couldn't find - " << integer << " in the array" << endl;
}

int main() {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto arr_mult_by2 = multiplyByTwo(arr);

    findInteger(arr);
    findInteger(arr_mult_by2);

    return EXIT_SUCCESS;
}

Output:

found - 10 in the array
found - 10 in the array
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Vector

  • Print Out the Contents of a Vector in C++
  • Access Member Functions From Pointer to a Vector in C++