zoukankan      html  css  js  c++  java
  • c++ distance 和advance函数

     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

     

     

  • 相关阅读:
    sed&awk 资料汇总 全是链接
    LeetCode Path 3Sum
    C++ mem_fun
    递归绑定
    查询当天数据
    清除script注入
    防注入查询
    我的最新分页
    群发邮件
    利用缓存
  • 原文地址:https://www.cnblogs.com/youxin/p/2552262.html
Copyright © 2011-2022 走看看