📄 rev_5984.htm
字号:
<HTML><TITLE>reverse_bidirectional_iterator, reverse_iterator</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_bidirectional_iterator, reverse_iterator</H2>
<HR><PRE> Iterator</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>An iterator that traverses a collection backwards.</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="#Interface"><LI>Interface</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 <iterator></PRE>
<PRE>
template <class BidirectionalIterator,
class T,
class Reference = T&,
class Pointer = T*
class Distance = ptrdiff_t>
class <B>reverse_bidirectional_iterator</B> : public
bidirectional_iterator<T, Distance> ;
template <class RandomAccessIterator,
class T,
class Reference = T&,
class Pointer = T*,
class Distance = ptrdiff_t>
class <B>reverse_iterator</B> : public random_access_iterator<T, Distance>;</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The iterators <B><I>reverse_iterator</B></I> and<B><I> reverse_bidirectional_iterator</B></I> correspond to <A HREF="Ran_7047.htm"><B><I>Random Access Iterators</B></I></A> and <A HREF="Bid_7861.htm"><B><I>Bidirectional Iterators</B></I></A>, except they traverse the collection they point to in the opposite direction. The fundamental relationship between a reverse iterator and its corresponding iterator <SAMP>i</SAMP> is established by the identity: </P>
<PRE>&*(reverse_iterator(i)) == &*(i-1); </PRE>
<PRE></PRE><P>This mapping is dictated by the fact that, while there is always a pointer past the end of a container, there might not be a valid pointer before its beginning. </P>
<P>The following are true for <B><I>reverse_bidirectional_iterators</B></I> :</P>
<UL><LI><P>These iterators may be instantiated with the default constructor or by a single argument constructor that initializes the new <SAMP>reverse_bidirectional_iterator</SAMP> with a <SAMP>bidirectional_iterator</SAMP>.</P>
</LI>
<LI><P><SAMP>operator*</SAMP> returns a reference to the current value pointed to.</P>
</LI>
<LI><P><SAMP>operator++</SAMP> advances the iterator to the previous item (<SAMP>--current</SAMP>) and returns a reference to <SAMP>*this</SAMP>.</P>
</LI>
<LI><P><SAMP>operator++(int)</SAMP> advances the iterator to the previous item (<SAMP>--current</SAMP>) and returns the old value of <SAMP>*this</SAMP>.</P>
</LI>
<LI><P><SAMP>operator--</SAMP> advances the iterator to the following item (<SAMP>++current</SAMP>) and returns a reference to <SAMP>*this</SAMP>.</P>
</LI>
<LI><P><SAMP>operator--(int)</SAMP> Advances the iterator to the following item (<SAMP>++current)</SAMP> and returns the old value of <SAMP>*this.</SAMP></P>
</LI>
<LI><P><SAMP>operator== </SAMP>This non-member operator returns <SAMP>true</SAMP> if the iterators <SAMP>x</SAMP> and <SAMP>y</SAMP> point to the same item.</P>
</LI>
</UL>
<P>The following are true for <B><I>reverse_iterators</B></I>:</P>
<UL>
<LI><P>These iterators may be instantiated with the default constructor or by a single argument constructor which initializes the new <SAMP>reverse_iterator</SAMP> with a <SAMP>random_access_iterator</SAMP>. </P>
<LI><P><SAMP>operator*</SAMP> returns a reference to the current value pointed to.</P>
<LI><P><SAMP>operator++</SAMP> advances the iterator to the previous item (<SAMP>--current</SAMP>) and returns a reference to <SAMP>*this</SAMP>.</P>
<LI><P><SAMP>operator++(int)</SAMP> advances the iterator to the previous item (<SAMP>--current</SAMP>) and returns the old value of <SAMP>*this</SAMP>.</P>
<LI><P><SAMP>operator--</SAMP> advances the iterator to the following item (<SAMP>++current</SAMP>) and returns a reference to <SAMP>*this</SAMP>.</P>
<LI><P><SAMP>operator--(int)</SAMP> advances the iterator to the following item (<SAMP>++current</SAMP>) and returns the old value of <SAMP>*this</SAMP>.</P>
<LI><P><SAMP>operator==</SAMP> is a non-member operator returns <SAMP>true</SAMP> if the iterators <SAMP>x</SAMP> and <SAMP>y</SAMP> point to the same item.</P>
<LI><P>The remaining operators (<SAMP><, +, - , +=, -=</SAMP>) are redefined to behave exactly as they would in a <SAMP>random_access_iterator</SAMP>, except with the sense of direction reversed.</P></LI></UL>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>All iterator operations are required to take at most amortized constant time.</P>
<A NAME="Interface"><H3>Interface</H3></A>
<PRE>template <class BidirectionalIterator,
class T,
class Reference = T&,
class Pointer = T*,
class Distance = ptrdiff_t>
class reverse_bidirectional_iterator
: public bidirectional_iterator<T, Distance> {
typedef reverse_bidirectional_iterator<BidirectionalIterator, T,
Reference,
Pointer, Distance> self;
friend bool operator== (const self&, const self&);
public:
reverse_bidirectional_iterator ();
explicit reverse_bidirectional_iterator
(BidirectionalIterator);
BidirectionalIterator base ();
Reference operator* ();
self& operator++ ();
self operator++ (int);
self& operator-- ();
self operator-- (int);
};
// Non-member Operator
template <class BidirectionalIterator,
class T,
class Reference,
class Pointer
class Distance>
bool operator== (
const reverse_bidirectional_iterator
<BidirectionalIterator,
T,
Reference,
Pointer
Distance>&,
const reverse_bidirectional_iterator
<BidirectionalIterator,
T,
Reference,
Pointer,
Distance>&);
template <class RandomAccessIterator,
class T,
class Reference = T&,
class Pointer = T*,
class Distance = ptrdiff_t>
class reverse_iterator
: public random_access_iterator<T, Distance> {
typedef reverse_iterator<RandomAccessIterator, T, Reference,
Pointer, Distance> self;
friend bool operator== (const self&, const self&);
friend bool operator< (const self&, const self&);
friend Distance operator- (const self&, const self&);
friend self operator+ (Distance, const self&);
public:
reverse_iterator ();
explicit reverse_iterator (RandomAccessIterator);
RandomAccessIterator base ();
Reference operator* ();
self& operator++ ();
self operator++ (int);
self& operator-- ();
self operator-- (int);
self operator+ (Distance) const;
self& operator+= (Distance);
self operator- (Distance) const;
self& operator-= (Distance);
Reference operator[] (Distance);
};
// Non-member Operators
template <class RandomAccessIterator, class T,
class Reference, class Pointer,
class Distance> bool operator== (
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&,
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&);
template <class RandomAccessIterator, class T,
class Reference, class Pointer,
class Distance> bool operator< (
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&,
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&);
template <class RandomAccessIterator, class T,
class Reference, class Pointer,
class Distance> Distance operator- (
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&,
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&);
template <class RandomAccessIterator, class T,
class Reference, class Pointer,
class Distance>
reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance> operator+ (
Distance,
const reverse_iterator<RandomAccessIterator, T,
Reference, Pointer,
Distance>&);
</PRE>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// rev_itr.cpp
//
#include <iterator>
#include <vector>
#include <iostream.h>
int main()
{
//Initialize a vector using an array
int arr[4] = {3,4,7,8};
vector<int> v(arr,arr+4);</PRE><PRE> //Output the original vector
cout << "Traversing vector with iterator: " << endl << " ";
for(vector<int>::iterator i = v.begin(); i != v.end(); i++)
cout << *i << " ";</PRE><PRE> //Declare the reverse_iterator
vector<int>::<B>reverse_iterator</B> rev(v.end());
vector<int>::<B>reverse_iterator</B> rev_end(v.begin());</PRE><PRE> //Output the vector backwards
cout << endl << endl;
cout << "Same vector, same loop, reverse_itertor: " << endl << " ";</PRE>
<PRE> for(; rev != rev_end; rev++)
cout << *rev << " ";</PRE><PRE> return 0;
}
Output :
Traversing vector with iterator:
3 4 7 8
Same vector, same loop, reverse_itertor:
8 7 4 3</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="Ite_5295.htm"><B><I>Iterators</B></I></A></P>
<HR>
<A HREF="rev_6652.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="rev_6851.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -