reverse/reverse_copy详解

reverse:逆转元素次序               

//TEMPLATE FUNCTION reverse

template<class_BidIt> inline

         void_Reverse(_BidIt _First, _BidIt _Last, bidirectional_iterator_tag)

         {       // reverse elements in [_First, _Last), bidirectionaliterators

         for (;_First != _Last && _First != --_Last; ++_First)

                   _STD iter_swap(_First,_Last);//这里居然没有使用反向迭代器.

         }

reverse_copy:逆转元素,并返回逆转后的容器

                   //TEMPLATE FUNCTION reverse_copy

template<class _BidIt,

         class_OutIt> inline

         _OutIt _Reverse_copy(_BidIt _First,_BidIt _Last,

                   _OutIt _Dest)

         {       // copy reversing elements in [_First, _Last)

         for (;_First != _Last; ++_Dest)

                   *_Dest = *--_Last;

         return(_Dest);

         }

举例:

int main()

{

         typedefvector<int> vecIntContains;

         vecIntContains vecInt;

         vecInt.push_back( 2 );

         vecInt.push_back( 7 );

         vecInt.push_back( 7 );

         vecInt.push_back( 5 );

         vecInt.push_back( 4 );

         vecInt.push_back( 1 );

         vecInt.push_back( 4 );

         vecInt.push_back( 1 );

         vecIntContains vecNew;

         copy(vecInt.begin(),vecInt.end(),ostream_iterator<int>(cout," " ) );

         cout<<"\nvecIntfirst:\n";

         reverse( vecInt.begin(),vecInt.end() );//left > right

         copy(vecInt.begin(),vecInt.end(),ostream_iterator<int>(cout," " ) );

 

         cout<<"\nuniquesecond operator:\n";

         reverse_copy(vecInt.begin(),vecInt.end(),ostream_iterator<int>(cout," " ));//left > right

         system( "pause");

         return0;

}

 

你可能感兴趣的:(function,iterator,System,Class)