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. 用hexo书写github.io博客 学习心得 教程

    很久没更新文章了,除了工作忙之外,可能就是自己懒惰了. 最近混迹与github,发现git上写博客也是个很不错的平台. 推荐使用 hexo 模版来书写,毕竟我们重点是写文章,而不是管理,所以有神奇何妨 ...

  2. 【转】phpcms-v9中关于模型的理解

    PHPCMS v9 模型概念 一.什么是模型? 模型是系统知识的抽象表示.我们不能仅仅通过语言来描述一个系统,也不能仅仅通过记忆来记录关于系统的知识.知识是通过某种媒介来表达的,这种媒介所表达的内容就 ...

  3. Caffe学习系列(15):添加新层

    如何在Caffe中增加一层新的Layer呢?主要分为四步: (1)在./src/caffe/proto/caffe.proto 中增加对应layer的paramter message: (2)在./i ...

  4. explicit,violate,volatile,mutable小结

    转自:http://blog.csdn.net/helonsy/article/details/7091130 explicit:放在构造函数前面可以阻止构造函数的隐式类型转换.这样可以避免不必要的错 ...

  5. php curl 实例+详解

    直接上实例 <?php //创建一个新cURL资源 $ch = curl_init(); //用于中文等特殊字符的url转码 $aurl = urlencode($address); $url= ...

  6. COGS 2188. [HZOI 2015] Math 题解

      题目描述: 给定n个数X1-Xn,求下面式子的值(整数部分): n<=107,xi<=109且互不相同. 分析: 其实一开始看见这道题我也吓傻了,k这么大,再说我又是数论鶸渣,打死也不 ...

  7. Flask 模板语言

    Flask使用的是Jinja2模板引擎 举个例子: from flask import Flask, render_template app = Flask(__name__) @app.route( ...

  8. DOM高级

    表格应用 获取 tBodies, tHead, tFoot, rows, cells 隔行变色 鼠标移入高亮, 添加,删除一行 DOM的方法使用 <!DOCTYPE html PUBLIC &q ...

  9. 基于HTK语音工具包进行孤立词识别的使用教程

    选自:http://my.oschina.net/jamesju/blog/116151 1前言 最近一直在研究HTK语音识别工具包,前几天完成了工具包的安装编译和测试,这几天又按耐不住好奇,决定自己 ...

  10. 不懂点CAP理论,你好意思说你是做分布式的吗?