Use std::swap between vectors or vector::swap?

Assuming a sane implementation, both of those functions should be implemented identically. So you should use whatever is most readable in your code.

In particular, if we look at the description for std::swap(vector<T> & x, vector<T> & y), it's effect is x.swap(y).


You should not use std::swap() directly in any case! Instead, you should use something like this:

using std::swap;
swap(x, y);

For std::vector<...> it probably doesn't make a difference as std::vector<...> obviously lives in namespace std. Otherwise the key difference is that with using std::swap() the default implementation is being used while with the approach outlined about ADL can find a better version.

Using swap(x, y) for std::vector<...>s x and y will just call x.swap(y). For consistency with other uses I would use the approach listed above.


References:

  • How does "using std::swap" enable ADL?
  • what does `using std::swap` inside the body of a class method implementation mean?