.\" ident @(#)reverse.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH reverse 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2reverse\fP \ - Reverses the order of elements in a collection. .SH SYNOPSIS .br #include .br template .br void reverse (BidirectionalIterator first, .RE .RS 13 BidirectionalIterator last); .SH DESCRIPTION The algorithm reverse reverses the elements in a sequence so that the last element becomes the new first element, and the first element becomes the new last. For each non-negative integer \f2i <= (last - first)/2\fP, reverse applies swap to all pairs of iterators \f2first + I, (last - I) - 1\fP. Because the iterators are assumed to be bidirectional, reverse does not return anything. .SH COMPLEXITY reverse performs exactly \f2(last - first)/2\fP swaps. .SH EXAMPLE .RE .RS 0 // .br // reverse.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br int main() .RE .RS 1 { .RE .RS 3 //Initialize a vector with an array of ints .RE .RS 2 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; .br vector v(arr, arr+10); .RE .RS 0 .RE .RS 3 //Print out elements in original (sorted) order .RE .RS 2 cout << "Elements before reverse: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl << endl; .RE .RS 0 .RE .RS 3 //Reverse the ordering .br reverse(v.begin(), v.end()); .RE .RS 0 .RE .RS 3 //Print out the reversed elements .RE .RS 2 cout << "Elements after reverse: " << endl << " "; .RE .RS 0 .RE .RS 2 copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br Elements before reverse: .RE .RS 4 1 2 3 4 5 6 7 8 9 10 .RE .RS 0 Elements after reverse: .RE .RS 4 10 9 8 7 6 5 4 3 2 1 .RE .RS 0 A reverse_copy to cout: .RE .RS 4 1 2 3 4 5 6 7 8 9 10 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO reverse_copy, swap