.\" ident @(#)distance.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH distance 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2distance\fP \ - Computes the distance between two iterators. .SH SYNOPSIS .RE .RS 0 #include .br template .br iterator_traits::difference_type .br distance (ForwardIterator first, .RE .RS 15 ForwardIterator last); .RE .RS 0 .br template .br void distance (ForwardIterator first, .RE .RS 15 ForwardIterator last, .br Distance& n); .SH DESCRIPTION The distance template function computes the distance between two iterators. The first version returns that value, while the second version increments \f2n\fP by that value. The last iterator must be reachable from the first iterator. Note that the second version of this function is obsolete. It is included for backward compatibility and to support compilers that do not include partial specialization. The first version of the function is not available with compilers that do not support partial specialization, since it depends on \f2iterator_traits\fP, which itself depends on that particular language feature. .SH EXAMPLE .RE .RS 0 // .br // distance.cpp .br // .br .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br int main() .RE .RS 1 { .RE .RS 3 // .br //Initialize a vector using an array .br // .RE .RS 2 int arr[6] = {3,4,5,6,7,8}; .br vector v(arr,arr+6); .RE .RS 3 // .br //Declare a list iterator, s.b. a ForwardIterator .br // .RE .RS 2 vector::iterator itr = v.begin()+3; .RE .RS 3 // .br //Output the original vector .br // .RE .RS 2 cout << "For the vector: "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl << endl; .RE .RS 0 .RE .RS 2 cout << "When the iterator is initialized to point to " .RE .RS 8 << *itr << endl; .RE .RS 3 // .br // Use of distance .br // .RE .RS 2 vector::difference_type dist = 0; .br distance(v.begin(), itr, dist); .br cout << "The distance between the beginning and itr is " .RE .RS 8 << dist << endl; .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br For the vector: 3 4 5 6 7 8 .br When the iterator is initialized to point to 6 .br The distance between the beginning and itr is 3 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector >\fP instead of: \f2vector \fP If your compiler does not support partial specialization, then you can't use the version of distance that returns the distance. Instead you have to use the version that increments a reference parameter. If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO Sequences, Random_Access_Iterators