C/C++ Arena

Step 3 of 5

const references

Passing a std::string or a big object by value copies it. Passing by reference avoids the copy, and adding const promises you won't modify it. This is the everyday C++ way to take read-only parameters:

int count_spaces(const std::string& s);

Rule of thumb: small types (int, double, char) by value, everything bigger by const&.

Your turn: write bool starts_with(const std::string& s, const std::string& prefix) without using any library starts_with.

Previous: Pass by reference Next: Overloading