I tried to do an excercise from Stroupstrup's book. The reader is asked to reverse the
order of elements in a vector so that if originally vector[0] = 1, vector [1] = 2, and
vector[2] = 3, then new vector has newvec[0] = 3, newvec[1] = 2, and newvec[2] = 1.
Doing this through creating a new vector was straighforward. However, part b) of the
exercise requests the reader to reverse the elements WITHOUT creating a new vector.
I guess it means to reverse the elements of the original vector. Stroupstrup suggests
that the reader use the swap function to do this.
I researched the swap function and can't find any examples of it being used with ONE
vector. It is always used with two vectors as in
first.swap(second);
.
I thought that it might be used for swapping elements as in
first[i].swap(first[first.size() - 1 -i]);
.
But this gave me a memory allocation error.
This exercise is from the chapter (8) in which Stroustrup explains pass-by-reference.
I thought of trying to use pass-by-reference for swapping but I don't see how to formulate
that construction.
How does one use the swap function to reverse the order of elements in a vector without
using any other vectors?