blob: 3ca2e94b254d7dbcdb5eb2f5afa7b7cce683f031 [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>swap_ranges()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="swap.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="time-get.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>swap_ranges()</H2>
<P><B>Library:</B>&nbsp;&nbsp;<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 a range of values in one location with those in another</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;algorithm&gt;
namespace std {
template &lt;class ForwardIterator1, class ForwardIterator2&gt;
ForwardIterator2
swap_ranges(ForwardIterator1 start1,
ForwardIterator1 finish1,
ForwardIterator2 start2);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>swap_ranges()</SAMP> algorithm exchanges corresponding values in two ranges, in the following manner:</P>
<P>For each non-negative integer <SAMP>n &lt; (finish - start)</SAMP>, the function exchanges <SAMP>*(start1 + n)</SAMP> with <SAMP>*(start2 + n))</SAMP>. After completing all exchanges, <SAMP>swap_ranges()</SAMP> returns an iterator that points to the end of the second container (in other words, <SAMP>start2 + (finish1 -start1)</SAMP>). The result of <SAMP>swap_ranges()</SAMP> is undefined if the two ranges <SAMP>[start, finish)</SAMP> and <SAMP>[start2, start2 + (finish1 - start1))</SAMP> overlap. </P>
<A NAME="sec5"><H3>Example</H3></A>
<UL><PRE>//
// swap.cpp
//
#include &lt;algorithm&gt; // for copy, iter_swap
#include &lt;iostream&gt; // for cout, endl
#include &lt;iterator&gt; // for ostream_iterator
#include &lt;vector&gt; // for vector
int main ()
{
typedef std::ostream_iterator&lt;int, char,
std::char_traits&lt;char&gt; &gt; Iter;
const int a[] = { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 };
// Set up a vector
std::vector&lt;int, std::allocator&lt;int&gt; &gt;
v (a, a + sizeof a / sizeof *a);
// Output original vector.
std::cout &lt;&lt; "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 &lt;&lt; "\n\nSwapping the first five elements "
&lt;&lt; "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 &lt;&lt; "\n\nSwapping the first and last elements "
&lt;&lt; "gives: \n ";
std::copy (v.begin (), v.end (), Iter (std::cout, " "));
std::cout &lt;&lt; std::endl;
return 0;
}
Program Output:
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>
<UL><PRE></PRE></UL>
<A NAME="sec6"><H3>See Also</H3></A>
<P><SAMP><A HREF="iter-swap.html">iter_swap()</A></SAMP>, <SAMP><A HREF="swap.html">swap()</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="swap.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="time-get.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>