.\" ident @(#)iter_swap.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH iter_swap 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2iter_swap\fP \ - Exchanges values in two locations. .SH SYNOPSIS .br #include .br template .br void iter_swap (ForwardIterator1, ForwardIterator2); .SH DESCRIPTION The iter_swap algorithm exchanges the values pointed to by the two iterators \f2a \fPand \f2b\fP. .SH EXAMPLE .br #include .br #include .br #include .br using namespace std; .br .br int main () .RE .RS 1 { .RE .RS 2 int d1[] = {6, 7, 8, 9, 10, 1, 2, 3, 4, 5}; .RE .RS 3 // .br // Set up a vector. .br // .RE .RS 2 vector v(d1+0, d1+10); .RE .RS 3 // .br // Output original vector. .br // .RE .RS 2 cout << "For the vector: "; .br copy(v.begin(), v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 3 // .br // Swap the first five elements with the .br // last five elements. .br // .RE .RS 2 swap_ranges(v.begin(), v.begin()+5, v.begin()+5); .RE .RS 3 // .br // Output result. .br // .RE .RS 2 cout << endl << endl .br << "Swapping the first 5 elements with the last 5 gives: " .RE .RS 8 << endl << " "; .RE .RS 2 copy(v.begin(), v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 3 // .br // Now an example of iter_swap -- swap first and .br // last elements. .br // .br iter_swap(v.begin(), v.end()-1); .br // .br // Output result. .br // .RE .RS 2 cout << endl << endl .RE .RS 8 << "Swapping the first and last elements gives: " .br << endl << " "; .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 For the vector: 6 7 8 9 10 1 2 3 4 5 .br Swapping the first five elements with the last five gives: .RE .RS 4 1 2 3 4 5 6 7 8 9 10 .RE .RS 0 Swapping the first and last elements gives: .RE .RS 4 10 2 3 4 5 6 7 8 9 1 .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 have 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 Iterators,_swap, swap_ranges