std::distance

来自cppreference.com

 
 
迭代器库
迭代器原语
Original:
Iterator primitives
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
iterator_traits
input_iterator_tag
output_iterator_tag
forward_iterator_tag
bidirectional_iterator_tag
random_access_iterator_tag
iterator
迭代器适配器
Original:
Iterator adaptors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reverse_iterator
流迭代器
Original:
Stream iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
istream_iterator
ostream_iterator
istreambuf_iterator
ostreambuf_iterator
迭代器操作
Original:
Iterator operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
advance
distance
prev(C++11)
next(C++11)
远程接入
Original:
Range access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
begin(C++11)
end(C++11)
 
Defined in header <iterator>
template< class InputIt >

typename std::iterator_traits<InputIt>::difference_type

    distance( InputIt first, InputIt last );
返回的元素数之间firstlast.
Original:
Returns the number of elements between first and last.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
的行为是不确定的,如果last是不可达的,从first可能反复递增first.
Original:
The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 参数

first -
迭代器,指向第一个元素
Original:
iterator pointing to the first element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
last -
迭代器,指向的最后一个元素
Original:
iterator pointing to the last element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Type requirements
-
InputIt must meet the requirements of InputIterator. The operation is more efficient if InputIt additionally meets the requirements of RandomAccessIterator

[编辑] 返回值

的数量之间firstlast的元素,
Original:
The number of elements between first and last.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 复杂性

线性.
Original:
Linear.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
然而,如果InputIt附加RandomAccessIterator符合要求的,复杂度为恒定
Original:
However, if InputIt additionally meets the requirements of RandomAccessIterator, complexity is constant.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 为例

#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';
}

Output:

3

[编辑] 另请参阅

给定距离的迭代器
Original:
advances an iterator by given distance
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函数) [edit]