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 ...
随机推荐
- php实现图片缩放功能类
http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package applicatio ...
- 6. 终端工具Xmanager使用技巧
1. 新建绘画使用终端连接服务器 2. 设置终端类型和编码 3. 设置终端外观,包括字体颜色等等 4. 设置默认上传路径和下载路径
- unity3d教程游戏包含的一切文件导入资源
http://www.58player.com/blog-2327-954.html 导入资源 将文件增加至工程文件夹的资源 (Assets) 文件夹后,Unity 将自动检测文件.将任何资源 (As ...
- [HDU3709]Balanced Number
[HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...
- 文本比较算法三——SUNDAY 算法
SUNDAY 算法描述: 字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上 ...
- InputStream,String相互转化
String --> InputStream InputStream String2InputStream(String str){ ByteArrayInputStream stream = ...
- 报错:No package erlang available
问题 yum install erlang 报错:No package erlang available 同样的,如果我们在安装nginx的时候出现"No package nginx ava ...
- PyQt4多线程定时刷新控件
1.通过事件关联和线程关联的方法刷新控件 self.listview=updatelistview()self.listview.updateText.connect(self.viewlist) ...
- Git的维护(git gc和git fsck)
原文: http://gitbook.liuhui998.com/4_10.html 一.保证git良好的性能 在大的仓库中, git靠压缩历史信息来节约磁盘和内存空间. 压缩操作并不是自动进行的, ...
- MySQL表字段长度的限制
在MySQL建表时,遇到一个奇怪的现象: root::>CREATE TABLE tb_test ( ) NOT NULL, ) DEFAULT NULL, ) DEFAULT NULL, ) ...