Using std::swap (using a swap element) for vectors ( std::vector ) has O (1) complexity.
From the C ++ standard
void swap (vector & x);
10 Effects: Exchanges the contents and capacity () * of this with an x.
11 Difficulty: Constant time .
You can “swap arrays” with constant time if they were dynamically allocated using the new operator. In this case, you can really only change pointers pointing to the first elements of arrays.
for example
#include <iostream>
Output signal
0 1 2 3 4 5 6 7 8 9 5 6 7 8 9 0 1 2 3 4
In fact, the same operation is performed in std :: vector.
Vlad from Moscow
source share