reverse_iterator::operator[]
從位置的指定數目的 reverse_iterator 定址項目傳回項目位移的參考。
reference operator[](
difference_type _Off
) const;
參數
- _Off
從 reverse_iterator 位址的位移。
傳回值
在項目位移的參考。
備註
運算子會傳回 *(*this + _Off)。
範例
// reverse_iterator_ret_ref.cpp
// compile with: /EHsc
#include <iterator>
#include <algorithm>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
int i;
vector<int> vec;
for (i = 1 ; i < 6 ; ++i )
{
vec.push_back ( 2 * 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;
vector <int>::iterator pos;
pos = find ( vec.begin ( ), vec.end ( ), 8 );
reverse_iterator<vector<int>::iterator> rpos ( pos );
// Declare a difference type for a parameter
reverse_iterator<vector<int>::iterator>::difference_type diff = 2;
cout << "The iterator pos points to: " << *pos << "." << endl;
cout << "The iterator rpos points to: " << *rpos << "." << endl;
// Declare a reference return type & use operator[]
reverse_iterator<vector<int>::iterator>::reference refrpos = rpos [diff];
cout << "The iterator rpos now points to: " << refrpos << "." << endl;
}
需求
標題: <iterator>
命名空間: std