std::distance
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <iterator> 中定义
|
||
| template< class InputIt > typename std::iterator_traits<InputIt>::difference_type |
||
返回的元素数之间
first和last.原文:
Returns the number of elements between
first and last.的行为是不确定的,如果
last是不可达的,从first可能反复递增first.原文:
The behavior is undefined if
last is not reachable from first by (possibly repeatedly) incrementing first.目录 |
[编辑] 参数
| first | - | 迭代器,指向第一个元素
原文: iterator pointing to the first element |
| last | - | 迭代器,指向的最后一个元素
原文: iterator pointing to the last element |
| 类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。 The operation is more efficient if InputIt additionally meets the requirements of RandomAccessIterator
| ||
[编辑] 返回值
的数量之间
first和last的元素,原文:
The number of elements between
first and last.[编辑] 复杂度
线性.
然而,如果
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 distance = std::distance(v.begin(), v.end()); std::cout << distance << '\n'; }
输出:
3
[编辑] 另请参阅
| 给定距离的迭代器 原文: advances an iterator by given distance (函数) | |