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. Windows下MySQL 5.6安装及配置详细图解

    一.安装前的准备 1.下载安装程序包,可到MySQL官方网站http://www.mysql.com/下载,如图1-1: 图1-1 下载后的安装文件如图1-2所示: 图1-2 二.安装 1.双击下载的 ...

  2. mysql基础面试

    php面试题之五--MySQL数据库(基础部分) 五.MySQL数据库 mysql_num_rows() mysql_affected_rows() 这两个函数都作用于 mysql_query($qu ...

  3. HDU 3743 Frosh Week(归并排序求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...

  4. Linux之ls命令

    s 命令可以说是linux下最常用的命令之一. -a 列出目录下的所有文件,包括以 . 开头的隐含文件.-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出.-c 输出文件 ...

  5. JavaScript深入浅出3-语句

    慕课网教程视频地址:Javascript深入浅出 程序由语句组成,语句遵守特定语法规则 块 block  {}   没有块级作用域 声明    var 异常   try catch finally 函 ...

  6. bug-android之INSTALL_FAILED_NO_MATCHING_ABIS

    bug描述: 经常在网络上下载一些实例,自己研究 ,运行时不时会出现这个bug: Installation error: INSTALL_FAILED_NO_MATCHING_ABIS bug解决方案 ...

  7. pip/matplot/pandas的安装和使用

    pip可以很方便的安装python的各种工具库,如pandas,matplotlib,scikit等,最大优点是它会自动解决库之间的依赖性,把所有需要的库都安装好,比起手工一个一个安装方便多了. 1. ...

  8. ubuntu14.04安装django

    1) sudo apt-get install python-pip#安装pip 2) pip install Django==1.8.1

  9. [k]web页面-browser兼容问题-1

    1:空的a标签在IE7/8下不能点击(2015-05-22) html代码: <ul class='oUl'><li><a href="#"> ...

  10. MyBatis <if>标签的一些问题

    1.常见错误: There is no getter for property named 'parentId' in 'class java.lang.Long'(或者String) org.myb ...