advance
依指定的位置數目遞增迭代器。
template<class InputIterator, class Distance>
void advance(
InputIterator& InIt,
Distance Off
);
參數
InIt
要遞增且必須符合輸入迭代器需求的迭代器。Off
整數類資料類型,可以轉換為迭代器的差異類型,並指定迭代器位置向前移的增量數目。
備註
這個前移範圍必須為非奇異,而迭代器必須是可取值或超出結尾。
如果 InputIterator 滿足雙向迭代器的需求,則 Off 可以是負數。 如果 InputIterator 是輸入或正向迭代器類型,Off 必須是非負值。
當 InputIterator 滿足隨機存取迭代器的需求時,advance 函式有常數複雜度,否則,它具有線性複雜度,因此可能會耗用相當多的資源。
範例
// iterator_advance.cpp
// compile with: /EHsc
#include <iterator>
#include <list>
#include <iostream>
int main( )
{
using namespace std;
int i;
list<int> L;
for ( i = 1 ; i < 9 ; ++i )
{
L.push_back ( i );
}
list <int>::iterator L_Iter, LPOS = L.begin ( );
cout << "The list L is: ( ";
for ( L_Iter = L.begin( ) ; L_Iter != L.end( ); L_Iter++)
cout << *L_Iter << " ";
cout << ")." << endl;
cout << "The iterator LPOS initially points to the first element: "
<< *LPOS << "." << endl;
advance ( LPOS , 4 );
cout << "LPOS is advanced 4 steps forward to point"
<< " to the fifth element: "
<< *LPOS << "." << endl;
advance ( LPOS , -3 );
cout << "LPOS is moved 3 steps back to point to the "
<< "2nd element: " << *LPOS << "." << endl;
}
需求
標頭:<iterator>
命名空間: std