Say you have two integer vectors:


I would like to define a function that allows me to swap a range of elements among the two vectors passing start index and lenght of the two sequences as arguments.
For instance:
where
and
are vectors and the numbers passed as arguments represents starting index and lenght of the sequences.
In this case I should get as autput
v1 = 1,2, 13,14,15 ,5,6,7,8,9
v2 = 10,11,12, 3,4 ,16,17,18
the signature of the function I defined as an example is not a constraint, if you think there is a better way it is ok
It appears that all the regular STL algorithms fall short of what you want to do exactly:
std::swap_rangeswould be almost there, but it requires that you swap equally long rangesstd::rotatewould also not be bad, but it requires that the end point of one range equals the begin point of the second range.You can of course easily abstract this into a function template that takes arbitrary iterator boundaries for your two ranges.
Edit based on David’s comment, you can do some optimization to avoid needless resizing
Update: it would be easier if you used
std::listsince then you could usesplice(I rearranged theinsert/erasepart there to mimic the code below)