| <HTML> |
| <HEAD> |
| <TITLE>iter_swap()</TITLE> |
| <LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD> |
| <BODY BGCOLOR=#FFFFFF> |
| <A HREF="isxdigit.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="iterator-h.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV> |
| <H2>iter_swap()</H2> |
| <P><B>Library:</B> <A HREF="2-9.html">Algorithms</A></P> |
| |
| <PRE><HR><B><I>Function</I></B><HR></PRE> |
| |
| <UL> |
| <LI><A HREF="#sec1">Local Index</A></LI> |
| <LI><A HREF="#sec2">Summary</A></LI> |
| <LI><A HREF="#sec3">Synopsis</A></LI> |
| <LI><A HREF="#sec4">Description</A></LI> |
| <LI><A HREF="#sec5">Example</A></LI> |
| <LI><A HREF="#sec6">See Also</A></LI> |
| <LI><A HREF="#sec7">Standards Conformance</A></LI> |
| </UL> |
| <A NAME="sec1"><H3>Local Index</H3></A> |
| No Entries |
| <A NAME="sec2"><H3>Summary</H3></A> |
| <P>Algorithm that exchanges values in two locations</P> |
| <A NAME="sec3"><H3>Synopsis</H3></A> |
| |
| <PRE>#include <algorithm> |
| |
| namespace std { |
| template <class ForwardIterator1, class ForwardIterator2> |
| void iter_swap(ForwardIterator1, ForwardIterator2); |
| } |
| </PRE> |
| <A NAME="sec4"><H3>Description</H3></A> |
| <P>The <SAMP>iter_swap()</SAMP> algorithm exchanges the values pointed to by the two iterators <SAMP>a </SAMP>and <SAMP>b</SAMP>.</P> |
| <A NAME="sec5"><H3>Example</H3></A> |
| |
| <UL><PRE>// |
| // swap.cpp |
| // |
| |
| #include <algorithm> // for copy, iter_swap |
| #include <iostream> // for cout, endl |
| #include <iterator> // for ostream_iterator |
| #include <vector> // for vector |
| |
| int main () |
| { |
| typedef std::ostream_iterator<int, char, |
| std::char_traits<char> > Iter; |
| |
| const int a[] = { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 }; |
| |
| // Set up a vector |
| std::vector<int, std::allocator<int> > |
| v (a, a + sizeof a / sizeof *a); |
| |
| // Output original vector. |
| std::cout << "For the vector: "; |
| std::copy (v.begin (), v.end (), Iter (std::cout, " ")); |
| |
| // Swap the first five elements with the last five elements |
| std::swap_ranges (v.begin (), v.begin () + 5, |
| v.begin () + 5); |
| |
| // Output result |
| std::cout << "\n\nSwapping the first five elements " |
| << "with the last five gives: \n "; |
| |
| std::copy (v.begin (), v.end (), Iter (std::cout," ")); |
| |
| // Now an example of iter_swap -- swap first |
| // and last elements. |
| std::iter_swap (v.begin(), v.end () - 1); |
| |
| // Output result |
| std::cout << "\n\nSwapping the first and last elements " |
| << "gives: \n "; |
| std::copy (v.begin (), v.end (), Iter (std::cout, " ")); |
| |
| std::cout << std::endl; |
| |
| return 0; |
| } |
| |
| |
| |
| Program Output: |
| </PRE></UL> |
| <UL><PRE>For the vector: 6 7 8 9 10 1 2 3 4 5 |
| |
| Swapping the first five elements with the last five gives: |
| 1 2 3 4 5 6 7 8 9 10 |
| |
| Swapping the first and last elements gives: |
| 10 2 3 4 5 6 7 8 9 1 |
| </PRE></UL> |
| <A NAME="sec6"><H3>See Also</H3></A> |
| <P><A HREF="iterators.html">Iterators</A>, <SAMP><A HREF="swap.html">swap()</A></SAMP>, <SAMP><A HREF="swap-ranges.html">swap_ranges()</A></SAMP></P> |
| <A NAME="sec7"><H3>Standards Conformance</H3></A> |
| <P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.2</I></P> |
| |
| <BR> |
| <HR> |
| <A HREF="isxdigit.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="iterator-h.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A> |
| |
| <!-- Google Analytics tracking code --> |
| <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </script> |
| <script type="text/javascript"> |
| _uacct = "UA-1775151-1"; |
| urchinTracker(); |
| </script> |
| <!-- end of Google Analytics tracking code --> |
| |
| </BODY> |
| </HTML> |