Bagikan melalui


operator- (<iterator>)

Subtracts one iterator from another and returns the difference.

template<class RandomIterator> 
   typename reverse_iterator<RandomIterator>::difference_type operator-(
      const reverse_iterator<RandomIterator>& _Left, 
      const reverse_iterator<RandomIterator>& _Right 
   );

Parameters

  • _Left
    An iterator that serves as the minuend from which another iterator is to be subtracted in forming the difference.

  • _Right
    An iterator that serves as the subtrahend that is to be subtracted from other iterator in forming the difference.

Return Value

The difference between two iterators: _Left - _Right.

Remarks

The difference equals the minuend minus the subtrahend.

Example

// iterator_op_sub.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   vector<int> vec;
   for (i = 0 ; i < 6 ; ++i )  
   {
      vec.push_back ( 2 * i );
   }
   
   vector <int>::iterator vIter;

   cout << "The initial vector vec is: ( ";
   for ( vIter = vec.begin( ) ; vIter != vec.end( ); vIter+)
      cout << *vIter << " ";
   cout << ")." << endl;

   vector <int>::reverse_iterator rVPOS1 = vec.rbegin ( ), 
          rVPOS2 = vec.rbegin ( );
   
   cout << "The iterators rVPOS1 & rVPOS2 initially point to "
        << "the first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

   for (i = 1; i < 5; ++i)  
   {
      rVPOS2++;
   }
   cout << "The iterator rVPOS2 now points to the fifth "
        << "element\n in the reversed sequence: "
        << *rVPOS2 << "." << endl;

   vector<int>::difference_type diff = rVPOS2 - rVPOS1;
   cout << "The difference: rVPOS2 - rVPOS1= "
        << diff << "." << endl;
}

The initial vector vec is: ( 0 2 4 6 8 10 ). The iterators rVPOS1 & rVPOS2 initially point to the first element in the reversed sequence: 10. The iterator rVPOS2 now points to the fifth element in the reversed sequence: 2. The difference: rVPOS2 - rVPOS1= 4.

Requirements

Header: <iterator>

Namespace: std

See Also

Reference

Standard Template Library

Other Resources

<iterator> Members