共用方式為


reverse_iterator::operator-

reverse_iterator 減去位移並傳回 reverse_iterator 處理項目在位移 (Offset) 位置。

reverse_iterator<RandomIterator> operator-(
   difference_type _Off
) const;

參數

  • _Off
    從 reverse_iterator 會減去的位移。

傳回值

解決 reverse_iterator 位移的項目。

備註

此成員函式,如果 reverse_iterator 滿足隨機存取 Iterator,的需求可能只能透過取得。

範例

// reverse_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 = 1 ; i < 6 ; ++i )
   {
      vec.push_back ( 3 * i );
   }
   
   vector <int>::iterator vIter;

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

   vector <int>::reverse_iterator rvIter;
   cout << "The vector vec reversed is: ( ";
   for ( rvIter = vec.rbegin( ) ; rvIter != vec.rend( ); rvIter++)
      cout << *rvIter << " ";
   cout << ")." << endl;

   // Initializing reverse_iterators to the first element
   vector <int>::reverse_iterator rVPOS1 = vec.rend ( ) - 1;
   
   cout << "The iterator rVPOS1 initially points to the last "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

   vector <int>::reverse_iterator rVPOS2 =rVPOS1 - 2; // offset subtracted
   cout << "After the -2 offset, the iterator rVPOS2 points\n"
        << " to the 2nd element from the last in the reversed sequence: "
        << *rVPOS2 << "." << endl;
}
  
  
  
  

需求

標題: <iterator>

命名空間: std

請參閱

參考

reverse_iterator Class

標準樣板程式庫