distance

두 반복기가 해결 위치 사이의 간격을 결정 합니다.

template<class InputIterator>
   typename iterator_traits<InputIterator>::difference_type
      distance(
         InputIterator _First, 
         InputIterator _Last
      );

매개 변수

  • _First
    해당 거리에서 두 번째 결정 하는 첫 번째 반복기입니다.

  • _Last
    두 번째 반복기 결정 하는 첫 번째에서 거리입니다.

반환 값

횟수는 _First 같은 것까지 증가 해야 _Last.

설명

Distance 함수 상수 복잡해졌습니다 때 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

참고 항목

참조

distance (STL Samples)

표준 템플릿 라이브러리