distance
确定数目有所增加在两个迭代器解决的位置之间的。
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance(
InputIterator _First,
InputIterator _Last
);
参数
_First
将确定第二个的距离的第一个迭代器。_Last
将确定距离后启动的第二个迭代器。
返回值
必须增加,直到达到其等同于 _Last_First 的次数。
备注
当 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;
}
要求
标头: <iterator>
命名空间: std