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 ...
随机推荐
- Java Io 对象序列化和反序列化
Java 支持将任何对象进行序列化操作,序列化后的对象文件便可通过流进行网络传输. 1. 对象序列化就是将对象转换成字节序列,反之叫对象的反序列化 2. 序列化流ObjectOut ...
- C# switch
要开学了(啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊) 做个沉默的行动者吧!(嘘嘘嘘嘘嘘嘘)今天去水题发现好多基础都不知道啊 1. switch(控制语句) { case 常量表达式:{statement ...
- ex6的选择器
前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuery的称赞,很多是由于jQuery方便的元素选择器 ...
- python 函数的文档字符串 docstrings
函数体的第一行可以是一个可选的字符串文本:此字符串是该函数的文档字符串,或称为docstring.(更多关于 docstrings 的内容可以在 文档字符串一节中找到.)有工具使用 docstring ...
- JS对于数据常见操作
var _mozi=['墨家','墨子','墨翟','兼爱非攻','尚同尚贤']; $.each(_mozi,function(key,val){//先key 后值 循环 console.log(ke ...
- 顺序栈的c++实现及利用其实现括号的匹配
#include<iostream>#include<cassert>#include<cstring>#include<string>using na ...
- BZOJ 1014: [JSOI2008]火星人prefix
Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...
- 1.AngularJS初探
1.需要什么前端开发环境 1)代码编辑工具 webstorm 2)断点调试工具 chrome插件Batarang 3)版本管理 tortoiseGit 4)代码合并和混淆工具 grunt-contri ...
- django的cookie 和session
Cookie 1.获取cookie: request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt=' ...
- [mysql]max_allowed_packet ,centos
在通过脚本向mysql写入大量测试数据时,出现这个问题,记录下: https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html 修改/et ...