Const References

References are useful even when a function doesn't change the vector at all. Passing by value copies the whole vector, with all its elements. With five numbers that doesn't matter, but with a vector of a million measurements, the program would copy a million numbers every time it calls the function. A reference isn't copied, so passing it is fast, however big the vector is.

But a plain reference has a downside: the function could change the vector, even by accident. And whoever reads int total(std::vector<int>& numbers) thinks the function changes the vector. That's why you add the word const before the type.

const (short for constant) means that the value must not change. You can use it with an ordinary variable too, when its value should never change in the program:

#include <iostream>

int main() {
    const int rounds = 3;
    rounds = 4;
    return 0;
}

This program doesn't compile:

main.cpp:5:12: error: cannot assign to variable 'rounds' with const-qualified type 'const int'

The compiler makes sure that nobody assigns anything to a const variable.

A const reference combines both: the function gets the vector without copying it, and it can't change it:

#include <iostream>
#include <vector>

int total(const std::vector<int>& numbers) {
    int sum = 0;
    for (int number : numbers) {
        sum = sum + number;
    }
    return sum;
}

int main() {
    std::vector<int> purchases = {120, 45, 80};
    std::cout << "Total: " << total(purchases) << std::endl;
    return 0;
}

This prints:

Total: 245

Read the parameter const std::vector<int>& numbers as "numbers is a reference to a vector of whole numbers that must not change". The vector purchases in main isn't const: the const of a parameter applies only inside the function. If the function tried to change the vector, for example with numbers[0] = 0;, compiling would end with the error cannot assign to return value because function 'operator[]' returns a const value. For numbers.push_back(5);, the error is no matching member function for call to 'push_back'.

So which kind of parameter should you choose?

  • Small values that the function only reads (int, double, char, bool): pass them by value, int count. Copying one number is as fast as passing a reference.
  • Vectors and text that the function only reads: pass them by const reference, const std::vector<int>& numbers, const std::string& name. You can call such a function with a value that isn't in a variable too: for example, the function void greet(const std::string& name) with text in quotes, greet("Eva").
  • Anything the function should change: pass it by reference without const, int& score, std::vector<int>& scores.

Instructions

The vector steps holds the number of steps for each day. The function total only reads the vector, and yet it gets a copy of it. Change its parameter to a const reference. The program still prints Total steps: 38100.

Below the function total, write a function most that also takes the vector as a const reference and returns its largest element. In main, below the first print line, print the text Most in a day: and the result of most(steps). The program prints Total steps: 38100 and then Most in a day: 12000.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

4/10