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.

 // advance example
 #include <iostream>     // std::cout
 #include <iterator>     // std::distance
 #include <list>         // std::list

 int main () {
   std::list<int> mylist;
   ; i<; i++) mylist.push_back (i*);

   std::list<int>::iterator first = mylist.begin();
   std::list<int>::iterator last = mylist.end();

   std::cout << "The distance is: " << std::distance(first,last) << '\n';

   ;
 }
template <class InputIterator, class Distance>
  void advance (InputIterator& it, Distance n);
Advance iterator

Advances the iterator it by n element positions.

Return value

none

 // advance example
 #include <iostream>     // std::cout
 #include <iterator>     // std::advance
 #include <list>         // std::list

 int main () {
   std::list<int> mylist;
   ; i<; i++) mylist.push_back (i*);

   std::list<int>::iterator it = mylist.begin();

   std::advance (it,);

   std::cout << "The sixth element in mylist is: " << *it << '\n';

   ;
 }

Output:

The sixth element in mylist is: 50
template <class ForwardIterator>
  ForwardIterator next (ForwardIterator it,
       typename iterator_traits<ForwardIterator>::difference_type n = 1);
Get iterator to next element

Returns an iterator pointing to the element that it would be pointing to if advanced n positions.

 // next example
 #include <iostream>     // std::cout
 #include <iterator>     // std::next
 #include <list>         // std::list
 #include <algorithm>    // std::for_each

 int main () {
   std::list<int> mylist;
   ; i<; i++) mylist.push_back (i*);

   std::cout << "mylist:";
   std::for_each (mylist.begin(),
                  std::next(mylist.begin(),),
                  [](int x) {std::cout << ' ' << x;} );

   std::cout << '\n';

   ;
 }

Output:

mylist: 0 10 20 30 40

template <class BidirectionalIterator> BidirectionalIterator prev (BidirectionalIterator it, typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Get iterator to previous element

Returns an iterator pointing to the element that it would be pointing to if advanced -n positions.

Return value

An iterator to the element n positions before it.

 // prev example
 #include <iostream>     // std::cout
 #include <iterator>     // std::next
 #include <list>         // std::list
 #include <algorithm>    // std::for_each

 int main () {
   std::list<int> mylist;
   ; i<; i++) mylist.push_back (i*);

   std::cout << "The last element is " << *std::prev(mylist.end()) << '\n';

   ;
 }

Output:

The last element is 90

STL_advance distance prev next的更多相关文章

  1. 微信小程序之巧妙的封装

    巧妙的封装 暴露一个访问地址xapp.config.js module.exports = { api_host: `https://a.squmo.com/yizu` } 继续引入,加暴露api.c ...

  2. STL 2—迭代器相关运算——advance(),distance(),next(),prev()

    迭代器的头文件中定义了4个实现迭代器模板的函数模板. 1.advance(iterator,num):将迭代器iterator 移动了num个位置 2.distance(iterator1,itera ...

  3. [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  4. [Swift]LeetCode849. 到最近的人的最大距离 | Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  5. Sword STL迭代器prev,next相关函数

    迭代器的头文件中定义了4个实现迭代器模板的函数模板. .advance(iterator,num):将迭代器iterator 移动了num个位置 .distance(iterator1,iterato ...

  6. [LeetCode] 849. Maximize Distance to Closest Person 最大化最近人的距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  7. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  8. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  9. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

随机推荐

  1. iOS开发——高级篇——如何集成支付宝SDK

    一.什么是支付宝 第三方支付平台 和内购非常相似内购是用户将钱付款给苹果,之后苹果分成给商户支付宝是用户将钱付款给支付宝,之后支付宝将钱转入我们的账户 使用支付宝前提购买的物品必须是和应用程序无关的. ...

  2. Activity的四个启动模式

    /** * Activity有四种启动模式(android:launchMode) * 分别是: * 1. standard(默认),可以不停的在栈中创建新的Activity * 2. singleT ...

  3. SSH-Struts第二弹:一个Form提交两个Action

    根据CSDN中的博客:http://blog.csdn.net/forwayfarer/article/details/3030259进行学习. 1.多个submit的Form表单页面 or 在jsp ...

  4. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  5. Java读写文件通用格式

    String path = "I:\\"; File file = new File(path + "user_id_pair.txt"); FileReade ...

  6. Droid4x设置代理抓包

    Droid4x也是基于virtualbox+x86架构的   代理设置 设置->WIFI->鼠标按住WiredSSID选项不放->修改网络->显示高级选项->代理-> ...

  7. Python分割list

    对于一个很大的列表,例如有超过一万个元素的列表,假如需要对列表中的每一个元素都进行一个复杂且耗时的计算,用单线程处理起来会很慢,这时有必要利用多线程进行处理,处理之前首先需要对大的列表进行分割,分割成 ...

  8. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解

    HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...

  9. emmet 太 hackble 了 。。。

    http://www.open-open.com/lib/view/open1451954899292.html

  10. int型整数中2进制中含有1的个数。

    int func(x) { int countx =0; while(x) { countx ++; x = x&(x-1); } return countx; } 解释下思路: 1.任何一个 ...