📄 rev_6851.htm
字号:
<HTML><TITLE>reverse_copy</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>reverse_copy</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Reverse the order of elements in a collection while copying them to a new collecton.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class BidirectionalIterator, class OutputIterator>
OutputIterator <B>reverse_copy</B> (BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>reverse_copy</B></I> algorithm copies the range <SAMP>[first, last)</SAMP> to the range <SAMP>[result, result + (last - first))</SAMP> such that for any non- negative integer<SAMP> i < (last - first)</SAMP>, the following assignment takes place:</P>
<PRE> *(result + (last - first) -i) = *(first + i)</PRE>
<PRE></PRE><P><B><I>reverse_copy</B></I> returns<SAMP> result + (last - first)</SAMP>. The ranges <SAMP>[first, last)</SAMP> and <SAMP>[result, result + (last - first))</SAMP> must not overlap. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>reverse_copy</B></I> performs exactly <SAMP>(last - first)</SAMP> assignments.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// reverse.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main ()
{
//
// Initialize a vector with an array of integers.
//
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr+0, arr+10);
//
// Print out elements in original (sorted) order.
//
cout << "Elements before reverse: " << endl << " ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
cout << endl << endl;
//
// Reverse the ordering.
//
reverse(v.begin(), v.end());
//
// Print out the reversed elements.
//
cout << "Elements after reverse: " << endl << " ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
cout << endl << endl;
cout << "A reverse_copy to cout: " << endl << " ";
<B>reverse_copy</B>(v.begin(), v.end(), ostream_iterator<int>(cout, " "));</PRE>
<PRE> cout << endl;
return 0;
}
Output :
Elements before reverse:
1 2 3 4 5 6 7 8 9 10
Elements after reverse:
10 9 8 7 6 5 4 3 2 1
A reverse_copy to cout:
1 2 3 4 5 6 7 8 9 10</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="rev_6652.htm"><B><I>reverse</B></I></A></P>
<HR>
<A HREF="rev_5984.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="rev_1561.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -