std::advance
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <iterator> 中定义
|
||
template< class InputIt, class Distance > void advance( InputIt& it, Distance n ); |
||
增量迭代器
it
n
元素.原文:
Increments given iterator
it
by n
elements.如果
n
为负,迭代器递减。在这种情况下,InputIt
的BidirectionalIterator
必须满足的要求,否则该行为是未定义.原文:
If
n
is negative, the iterator is decremented. In this case, InputIt
must meet the requirements of BidirectionalIterator
, otherwise the behavior is undefined.目录 |
[编辑] 参数
it | - | 迭代器是先进的
|
n | - | 数的元素
it 应该是先进的原文: number of elements it should be advanced |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
|
[编辑] 返回值
(无)
[编辑] 复杂度
线性.
然而,如果
InputIt
附加RandomAccessIterator
符合要求的,复杂度为恒定原文:
However, if
InputIt
additionally meets the requirements of RandomAccessIterator
, complexity is constant.[编辑] 示例
#include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> v{ 3, 1, 4 }; auto vi = v.begin(); std::advance(vi, 2); std::cout << *vi << '\n'; }
输出:
4
[编辑] 另请参阅
返回两个迭代器之间的距离 原文: returns the distance between two iterators (函数) |