iter_swap


Swap the two elements indicated by two iterators.

Swap the two elements referenced by a and b .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class ForwardIterator1, class ForwardIterator2 >
void iter_swap( ForwardIterator1 a, ForwardIterator2 b );

Complexity

Time and space complexity are both constant.

Example <ospace/osstd/examples/iterswp0.cpp>
#include <iostream>
#include <algorithm>

int numbers[ 6 ] = { 0, 1, 2, 3, 4, 5 };

void
main()
  {
  iter_swap( numbers, numbers + 3 );

  for ( int i = 0; i < 6; ++i )
    cout << numbers[ i ] << ` `;
  cout << "\n";
  }

3 1 2 0 4 5
Example <ospace/osstd/examples/iterswp1.cpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>

void
main()
  {
  vector< int > v1( 6 );
  for ( int i = 0; i < v1.size(); ++i )
    v1[ i ] = i;

  iter_swap( v1.begin(), v1.begin() + 3 );

  ostream_iterator< int > iter( cout, " " );
  copy( v1.begin(), v1.end(), iter );
  cout << "\n";
  }

3 1 2 0 4 5

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