Yes, this is perfectly safe to swap vectors of the same type.
Vector under the hood is just a few pointers pointing to the data the vector uses and “end” of the sequence. When you call swap you just exchange those pointers between the vectors. You don’t need to worry that the vectors are the same size because of this.
Vectors of different types cannot be swapped using swap
. You’d need to implement your own function that does the conversion and swapping.