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. jQuery Colorbox插件

    http://www.open-open.com/lib/view/open1338084606042.html jQuery Colorbox是一款非常好的内容播放插件.它集弹出层.幻灯片播放功能于 ...

  2. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  3. iOS开发——高级篇——iOS 中的 NSTimer

    以前的老代码在使用 NSTimer 时出现了内存泄露 NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: 1 2 3 ...

  4. 剑指Offer 二叉搜索树的后序遍历序列

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同.   思路: 后续遍历数组的尾部为根节点,前面的部分 ...

  5. android中返回键捕获处理

    在android平台上捕获Back键事件,主要用来处理返回的相关逻辑,下列几种方法都可以捕获,如下所示: 1.获取按钮按下事件,兼容android 1.0到android 2.1,重写onKeyDow ...

  6. memcache 与 mencached扩展的区别

    memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速 ...

  7. mysql的安装以及基本操作

    一.在Linux 下安装MySQL ubuntu 下可以直接使用apt-get . centos 下yum源有没有就不知道了. 1. sudo apt-get install mysql-server ...

  8. JDBC的应用

    JDBC的开发步骤: 1.  引入JDBC驱动架包 2.  程序中引入JDBC驱动类     3.  创建java与数据库的连接 4.  跟数据库交互:发送sql语句,接收数据库对sql语句的执行结果 ...

  9. PyQt4软件打包成exe文件

    使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...

  10. C# 复制(深拷贝、浅拷贝)

    Object.MemberwiseClone 方法 创建当前 Object 的浅表副本. protected Object MemberwiseClone() MemberwiseClone 方法创建 ...