distance主要是用来求两个迭代器之间的元素个数。
template<class InputIterator> typename iterator_traits<InputIterator>::difference_type distance (InputIterator first, InputIterator last);
Return distance between iterators
Calculates the number of elements between first and last.
If i is a Random Access Iterator, the function uses operator- to calculate this. Otherwise, the function uses the increase operator (operator++) repeatedly.
advance函数增加迭代器。
template <class InputIterator, class Distance>
void advance (InputIterator& i, Distance n);
Advance iterator
Advances the iterator i by n elements.
Distance:Distance is any numerical type able to represent distances between iterators of this type.
#include <iostream> #include <iterator> #include <list> using namespace std; int main () { list<int> mylist; for (int i=0; i<10; i++) mylist.push_back (i*10); list<int>::iterator first = mylist.begin(); list<int>::iterator last = mylist.end(); cout << "The distance is: " << distance(first,last) << endl;//输出10 list<int>::iterator it=mylist.begin(); advance(it,5); cout<<*it; //输出50 return 0; }
深入:
http://www.cnblogs.com/woodfish1988/archive/2007/02/27/658498.html