Vectors and Functions

A vector can be a function parameter, like an int or a string. The rules from the References lesson apply here too. Without &, the function gets a copy of the whole vector:

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

void addZero(vector<int> nums) {
    nums.push_back(0);
    cout << "Inside: " << nums.size() << "\n";
}

int main() {
    vector<int> nums = {5, 6};
    addZero(nums);
    cout << "Outside: " << nums.size() << "\n";
    return 0;
}

Output:

Inside: 3
Outside: 2

Inside the function, the copy got a third value. The vector in main did not change, and it still has 2 values. This is pass by value. C++ copies every value of the vector for the call, so with a big vector it also costs time and memory.

Changing the caller's vector

To change the vector of main, write & after the type. Now the parameter is another name for the caller's vector, and no copy is made:

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

void addZero(vector<int>& nums) {
    nums.push_back(0);
    cout << "Inside: " << nums.size() << "\n";
}

int main() {
    vector<int> nums = {5, 6};
    addZero(nums);
    cout << "Outside: " << nums.size() << "\n";
    return 0;
}

Output:

Inside: 3
Outside: 3

The same idea works in a range-based for loop. In for (int n : nums), each n is a copy, so changing n does not change the vector. With int&, each n is the value inside the vector:

vector<int> nums = {1, 2, 3};
for (int& n : nums) {
    n = n * 10;
}
for (int n : nums) {
    cout << n << " ";
}
cout << "\n";

Output:

10 20 30 

Reading without a copy

Most functions only read a vector, for example to add up its values. For them, use a const reference. There is no copy, and the function cannot change the vector by mistake. If it tries, the program does not compile:

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

int total(const vector<int>& nums) {
    nums[0] = 0;
    return 0;
}

int main() {
    vector<int> nums = {1, 2};
    cout << total(nums) << "\n";
    return 0;
}

Output:

main.cpp: In function ‘int total(const std::vector<int>&)’:
main.cpp:6:13: error: assignment of read-only location ‘(& nums)->std::vector<int>::operator[](0)’

The compiler calls nums[0] a "read-only location", because of const. A good rule: use const vector<int>& when the function only reads, vector<int>& when it must change the vector, and a plain copy almost never.

Returning a vector

A function can also make a new vector and return it. The return type is the vector type:

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

vector<int> evensOf(const vector<int>& nums) {
    vector<int> result;
    for (int n : nums) {
        if (n % 2 == 0) {
            result.push_back(n);
        }
    }
    return result;
}

int main() {
    vector<int> nums = {3, 8, 5, 2, 6};
    vector<int> evens = evensOf(nums);
    for (int n : evens) {
        cout << n << " ";
    }
    cout << "\n";
    return 0;
}

Output:

8 2 6 

evensOf reads nums and leaves it as it is. It returns a new vector with only the even values, in the same order.

In the exercise, doubleAll gets a copy of the vector, so its changes are lost. Fix its parameter so it doubles the numbers in the vector of main.