共用方式為


operator- (<iterator>)

從減去另一個 Iterator 並傳回其差異。

template<class RandomIterator1, class RandomIterator2>
    Tdiff operator-(
        const move_iterator<RandomIterator1>& _Left,
        const move_iterator<RandomIterator2>& _Right
    );
template<class RandomIterator1, class RandomIterator2>
    Tdiff operator-(
        const reverse_iterator<RandomIterator1>& _Left,
        const reverse_iterator<RandomIterator2>& _Right
);

參數

  • _Left
    Iterator。

  • _Right
    Iterator。

傳回值

兩個 Iterator.之間的差異

備註

第一個範本運算子傳回 _Left.base() - _Right.base()。

第二個範本運算子傳回 _Right.current - _Left.current。

Tdiff 取決於所傳回的運算式的型別。否則,它就是 RandomIterator1::difference_type。

範例

// 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;
}
  
  
  
  

需求

標題: <iterator>

命名空間: std

請參閱

參考

標準樣板程式庫