swap_ranges


Swap two ranges of items.

Swap the elements in the range [ first1, last1 ) with the elements in a range of the same size starting at first2 . Return an iterator positioned immediately after the last element in the second range.

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class ForwardIterator1, class ForwardIterator2 >
ForwardIterator2 swap_ranges
  (
  ForwardIterator1 first1,
  ForwardIterator1 last1,
  ForwardIterator2 first2
  );
  

Complexity

Time complexity is linear. Space complexity is constant.

Example <ospace/osstd/examples/swprnge1.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>

void
main()
  {
  char word1[ 6 ]; ::strcpy( word1, "Fight" );
  char word2[ 6 ]; ::strcpy( word2, "Texas" );
  cout << word1 << " " << word2 << "\n";

  swap_ranges( word1, word1 + ::strlen( word1 ), word2 );

  cout << word1 << " " << word2 << "\n";
  }

World Hello
Hello World

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.