STL_advance distance prev next
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance (InputIterator first, InputIterator last);
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);
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);
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);
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的更多相关文章
- 微信小程序之巧妙的封装
巧妙的封装 暴露一个访问地址xapp.config.js module.exports = { api_host: `https://a.squmo.com/yizu` } 继续引入,加暴露api.c ...
- STL 2—迭代器相关运算——advance(),distance(),next(),prev()
迭代器的头文件中定义了4个实现迭代器模板的函数模板. 1.advance(iterator,num):将迭代器iterator 移动了num个位置 2.distance(iterator1,itera ...
- [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 ...
- [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 ...
- Sword STL迭代器prev,next相关函数
迭代器的头文件中定义了4个实现迭代器模板的函数模板. .advance(iterator,num):将迭代器iterator 移动了num个位置 .distance(iterator1,iterato ...
- [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 ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
随机推荐
- hexo问题篇(偶尔抽抽疯)
hexo安安稳稳的跑了很久,然后 ....让人心碎的hexo问题,华丽丽的摔倒在坑里,只因update了hexo version最是哪一句 hexo server让人欲哭无泪 -问题场景 设备: Ma ...
- Xcode 7 App Transport Security has blocked a cleartext HTTP 报错解决办法
Xcode 7 创建新项目用到 UIWebView 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http:// ...
- 剑指Offer 从上往下打印二叉树(dfs)
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 用一个队列来辅助,先压入根节点,设置一个指针记录队列头位置,判断队头指针有没有孩子,有压入左右孩子,,,操作完一次,队头出 ...
- HackerRank savita-and-friends
Description 在一条边上求一个点,使得这个点到所有点的最长的最短距离 最短. \(n \leqslant 10^5\) Sol Dijkstra+扫描线+单调队列. 这个好像叫什么最小直径生 ...
- div里包含img底部必定多出空白的解决办法
研究了很久,自己写了js代码都解决不了.最后还是靠万能的网友解决了这一问题! 问题:adding.margin.border都设为0,无效.怎么样都多出3px. 解决方案: 1.设置div{ font ...
- Python中sorted()方法
Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...
- Tomcat部署Web应用的两种方式
WEB工程目录结构 部署方式一:此种方式部署,jsp页面修改后,不能动态更新,需要重新部署才能看到效果.不过可以配置动态更新实现. 部署方式二:此种方式部署,jsp修改后,直接在页面可以看到效果.(因 ...
- VMware12中CentOS7网络设置
VMware提供了三种将虚拟网卡和物理网卡捆绑起来的方式,即桥接(Bridge)模式,网络地址转换(Network Address Transformation, NAT)模式和主机(Host Onl ...
- POJ 2769
http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单 ...
- Appium+Robotframework实现Android应用的自动化测试-4:AppiumLibrary介绍和安装
Appium是个好东东,Android,iOS都支持,并且居然RobotFramework也支持Appium了,这就是本文要介绍的AppiumLibrary. 通过前面的文章大家知道可以使用多种语言来 ...