swap


Swap two values.

Swap the elements a and b .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class T >
void swap( T& a, T& b );

Complexity

Time and space complexity are constant.

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

void
main()
  {
  int a = 42;
  int b = 19;
  cout << "a = " << a << " b = " << b << "\n";

  swap( a, b );

  cout << "a = " << a << " b = " << b << "\n";
  }

a = 41 b = 19
a = 19 b = 42

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