142 lines
5.9 KiB
HTML
142 lines
5.9 KiB
HTML
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed
|
|
with this work for additional information regarding copyright
|
|
ownership. The ASF licenses this file to you under the Apache
|
|
License, Version 2.0 (the License); you may not use this file
|
|
except in compliance with the License. You may obtain a copy of
|
|
the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
implied. See the License for the specific language governing
|
|
permissions and limitations under the License.
|
|
|
|
Copyright 1999-2007 Rogue Wave Software, Inc.
|
|
-->
|
|
|
|
<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> <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 <algorithm>
|
|
|
|
namespace std {
|
|
template <class ForwardIterator1, class ForwardIterator2>
|
|
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 < (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 <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:
|
|
|
|
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>
|