reverse


Reverse the items in a sequence.

Reverse the order of the elements in the range [ first, last ).

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class BidirectionalIterator >
void reverse
  (
  BidirectionalIterator first,
  BidirectionalIterator last
  );
  

Complexity

Time complexity is linear, as ( last - first ) swaps are performed. Space complexity is constant.

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

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

void
main()
  {
  reverse( numbers, numbers + 6 );

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

5 4 3 2 1 0

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