Vectors and functions
Read · 1 of 4
Pass vectors with &
Passing a vector normally makes a full copy, which is slow for big lists. Pass it by reference instead: int total(const vector<int>& a).
const promises the function won't change it. Drop const when the function is meant to change the vector.
Careful with positions: valid ones are 0 to size − 1. a[a.size()] is outside the vector and can crash your program or give garbage.