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. Unity内存申请和释放

    转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shad ...

  2. 跟着百度学PHP[4]OOP面对对象编程-13-魔术方法__set(),__get(),__isset(),__unset()

    __set() 在对象访问私有成员的时候自动被调用,达到了给你看,但是不能给你修改的效果!(在对象访问一个私有的成员的时候就会自动的调用该魔术方法) __get() 方法用于获取私有属性值.(在设置私 ...

  3. 剑指Offer 二维数组中的查找

    题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路法一: * 矩阵是 ...

  4. 取得DIV的ID还是CLASS

    无论你想取得DIV的ID还是CLASS 最重要的是找到你想取值的DIV对象.要取得DIV对象的方法有很多.常用的有2个,一个是根据ID,用var div=document.getElementById ...

  5. [mysql]max_allowed_packet ,centos

    在通过脚本向mysql写入大量测试数据时,出现这个问题,记录下: https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html 修改/et ...

  6. C语言宏定义时#(井号)和##(双井号)的用法

    C语言中如何使用宏C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结. 关于#和## 在C语言的宏中,#的功能是将其后面 ...

  7. 【架构】RPC 使用 Haproxy、keepalive作为负载均衡

    参考资料: Haproxy+keepalived 高可用负载:  http://www.tuicool.com/articles/qY7Rz23 keepalived原理(主从配置+haproxy)及 ...

  8. c++ macro

     C++ Code  12345678910111213141516171819202122232425262728293031   /* version: 1.0 author: hellogise ...

  9. 一个不错的定位API网站

    2015年5月2日 15:36:31 星期六 http://www.haoservice.com/

  10. java获取本月或某月的第一天和最后一天

    获取某月的第一天和最后一天的日期 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Ca ...