-
Partial vector swapping.
I have two vectors of ints. I need to cut each of them at some arbitrary point and swap the last half of each. So, if I have:
Code:
a = {1, 2, 3, 4, 5, 6}
b = {7, 8, 9, 10, 11, 12}
and I swap at the middle, the resulting vectors would be:
Code:
a = {1, 2, 3, 10, 11, 12}
b = {7, 8, 9, 4, 5, 6}
Now, I could iterate through them and perform the swap to a temporary vector, but I was wondering if there was a function in the STL that would do this for me?
-
There is the std::swap_ranges() standard generic algorithm that you can use, e.g.,
Assuming that the vectors are of equal length:
Code:
const std::vector<int>::size_type mid = vec1.size() / 2;
std::swap_ranges(vec1.begin() + mid, vec1.end(), vec2.begin() + mid);