| <HTML> |
| <HEAD> |
| <TITLE>copy(), copy_backward()</TITLE> |
| <LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD> |
| <BODY BGCOLOR=#FFFFFF> |
| <A HREF="containers.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="count.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>copy(), copy_backward()</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">Complexity</A></LI> |
| <LI><A HREF="#sec6">Example</A></LI> |
| <LI><A HREF="#sec7">See Also</A></LI> |
| <LI><A HREF="#sec8">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 copies a range of elements</P> |
| <A NAME="sec3"><H3>Synopsis</H3></A> |
| |
| <PRE>#include <algorithm> |
| |
| namespace std { |
| template <class InputIterator, class OutputIterator> |
| OutputIterator copy(InputIterator start, |
| InputIterator finish, |
| OutputIterator result); |
| template <class BidirectionalIterator1, |
| class BidirectionalIterator2> |
| BidirectionalIterator2 |
| copy_backward(BidirectionalIterator1 start, |
| BidirectionalIterator1 finish, |
| BidirectionalIterator2 result); |
| } |
| </PRE> |
| <A NAME="sec4"><H3>Description</H3></A> |
| <P>The <SAMP>copy()</SAMP> algorithm copies values from the range specified by <SAMP>[start</SAMP>, <SAMP>finish)</SAMP> to the range specified by <SAMP>[result, result + (finish - start))</SAMP>. <SAMP>copy()</SAMP> returns <SAMP>result + (finish - start)</SAMP>. For each non-negative integer <SAMP>n < (finish - start)</SAMP>, <SAMP>copy()</SAMP> assigns <SAMP>*(start + n)</SAMP> to <SAMP>*(result + n)</SAMP>. The result of <SAMP>copy()</SAMP> is undefined if <SAMP>result</SAMP> is in the range <SAMP>[start, finish)</SAMP>.</P> |
| <P>Unless <SAMP>result</SAMP> is an insert iterator, <SAMP>copy()</SAMP> assumes that all iterators in the range <SAMP>[result + (finish - start)]</SAMP> are dereferenceable.</P> |
| <P>The <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> algorithm copies elements in the range specified by <SAMP>[start, finish)</SAMP> into the range specified by [<SAMP>result - (finish - start), result)</SAMP>, starting from the end of the sequence (<SAMP>finish-1</SAMP>) and progressing to the front (<SAMP>start</SAMP>). Note that <SAMP>copy_backward()</SAMP> does <I>not </I>reverse the order of the elements, it simply reverses the order of transfer. <SAMP>copy_backward()</SAMP> returns <SAMP>result - (finish - start)</SAMP>. You should use <SAMP>copy_backward()</SAMP> instead of <SAMP>copy()</SAMP> when <SAMP>finish</SAMP> is in the range <SAMP>[result - (finish - start), result)</SAMP>. For each positive integer <SAMP>n <= (finish - start)</SAMP>, <SAMP>copy_backward()</SAMP> assigns <SAMP>*(finish - n)</SAMP> to <SAMP>*(result - n)</SAMP>. The result of <SAMP>copy_backward()</SAMP> is undefined if <SAMP>result</SAMP> is in the range <SAMP>[start, finish)</SAMP>. <SAMP>copy_backward()</SAMP> should be used when <SAMP>finish</SAMP> is in the range <SAMP>[result - (finish - start), result)</SAMP>.</P> |
| <P>Unless <SAMP>result</SAMP> is an insert iterator, <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> assumes that all iterators in the range <SAMP>[result - (finish - start), result)</SAMP> are dereferenceable.</P> |
| <A NAME="sec5"><H3>Complexity</H3></A> |
| <P>Both <SAMP>copy()</SAMP> and <SAMP><A HREF="copy.html">copy_backward()</A></SAMP> perform exactly <SAMP>finish - start</SAMP> assignments.</P> |
| <A NAME="sec6"><H3>Example</H3></A> |
| |
| <UL><PRE>// |
| // copyex.cpp |
| // |
| |
| #include <algorithm> // for copy |
| #include <iostream> // for cout, endl, ostream_iterator |
| #include <vector> // for vector |
| |
| |
| |
| int main () |
| { |
| // Typedef for convenience. |
| typedef std::vector<short, std::allocator<short> > vector; |
| |
| const vector::value_type d1[] = { 1, 2, 3, 4 }; |
| const vector::value_type d2[] = { 5, 6, 7, 8 }; |
| |
| // Set up three vectors. |
| vector v1(d1 + 0, d1 + 4), |
| v2(d2 + 0, d2 + 4), |
| v3(d2 + 0, d2 + 4); |
| |
| // Set up one empty vector. |
| vector v4; |
| |
| // Copy v1 to v2. |
| std::copy(v1.begin(), v1.end(), v2.begin()); |
| |
| // Copy backwards v1 to v3. |
| std::copy_backward(v1.begin(), v1.end(), v3.end()); |
| |
| // Use insert iterator to copy into empty vector. |
| std::copy(v1.begin(), v1.end(), std::back_inserter(v4)); |
| |
| // Copy all four vectors to cout. |
| std::ostream_iterator<vector::value_type, char, |
| std::char_traits<char> > out(std::cout, " "); |
| |
| std::copy(v1.begin(), v1.end(), out); |
| std::cout << std::endl; |
| |
| std::copy(v2.begin(), v2.end(), out); |
| std::cout << std::endl; |
| |
| std::copy(v3.begin(), v3.end(), out); |
| std::cout << std::endl; |
| |
| std::copy(v4.begin(), v4.end(), out); |
| std::cout << std::endl; |
| |
| return 0; |
| } |
| |
| |
| Program Output: |
| </PRE></UL> |
| <UL><PRE>1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
| 1 2 3 4 |
| </PRE></UL> |
| <A NAME="sec7"><H3>See Also</H3></A> |
| <P><A HREF="algorithms.html">Algorithms</A>, <A HREF="algorithm-h.html"><algorithm></A></P> |
| <A NAME="sec8"><H3>Standards Conformance</H3></A> |
| <P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.1</I></P> |
| |
| <BR> |
| <HR> |
| <A HREF="containers.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="count.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> |