STL: swap

iter_swap

Exchanges two values referred to by a pair of specified iterators.

template<class ForwardIterator1, class ForwardIterator2>
   void iter_swap(
      ForwardIterator1 _Left,
      ForwardIterator2 _Right
   );

这个好像没有什么特别的,就是交换两个迭代器所指向的内容。

swap

The first override exchanges the values of two objects. The second override exchanges the values between two arrays of objects.

template<class Type>
   void swap(
      Type& _Left, 
      Type& _Right
   );
template<class Type, size_t N>
   void swap(
      Type (&_Left)[N],
      Type (&_Right)[N]
   );

注,这里的对象包括集合。所以可以swap两个vector,但是容器都提供了swap成员函数,所以最好使用其swap成员函数,性能上可能好一些.此外,第二个声明表明可以swap两个相同长度的数组

swap_ranges

Exchanges the elements of one range with the elements of another, equal sized range.

template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator2 swap_ranges(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2
   );

注:The complexity is linear with _Last1 –  _First1 swaps performed. If elements from containers of the same type are being swapped, them the swap member function from that container should be used, because the member function typically has constant complexity.即该函数既可以交换任意容器间的内容,但是如果是相同容器内的元素最好是使用容器对应的swap成员函数,它可以提供常数时间的复杂度。

原文地址:https://www.cnblogs.com/freewater/p/2947625.html