Returning by reference is pretty simple. It's also super cool and you'll come across uses for it time and again when you get really involved in this stuff.
e.g.:
Code:
int& GetSmallest(vector<int>& v)
{
assert(!v.empty());
vector<int>::iterator smallest = v.begin();
vector<int>::iterator it = smallest;
++it;
for (; it != v.end(); ++it)
{
if (*it < *smallest)
smallest = it;
}
return *smallest;
}
This returns not exactly the smallest value in the vector, but a reference to the smallest value in the vector. So you could do something like this:
Code:
// v contains {5, 3, 7, 4}
++GetSmallest(v);
// v contains {5, 4, 7, 4}