distance
2 つの反復子によってアドレス指定された位置の間のインクリメント数を決定します。
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance(
InputIterator _First,
InputIterator _Last
);
パラメーター
_First
2 番目の間隔が決定される最初の反復子。_Last
までの距離が決定される 2 番目の反復子。
戻り値
_First がインクリメントされなければまで、等号 _Lastする回数。
解説
InputIterator がランダム アクセス反復子の必要条件を満たす場合に関数に定数の複雑さがあります。; それ以外の場合に線形複雑さがあるため、可能性があります。
使用例
// iterator_distance.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 ( 2 * 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 , 7 );
cout << "LPOS is advanced 7 steps forward to point "
<< " to the eighth element: "
<< *LPOS << "." << endl;
list<int>::difference_type Ldiff ;
Ldiff = distance ( L.begin ( ) , LPOS );
cout << "The distance from L.begin( ) to LPOS is: "
<< Ldiff << "." << endl;
}
必要条件
ヘッダー: の <反復子>
名前空間: std