Distance c что это

от admin

Distance c что это

A function that computes the distance between two iterators

Synopsis

Description

The distance() function template computes the distance between two iterators. The first version returns that value, while the second version increments n by that value. The last iterator must be reachable from the first iterator.

Note that the second version of this function is obsolete. It is included for backward compatibility and to accommodate compilers that do not support partial specialization. The first version of the function is not available with compilers that do not support partial specialization, since it depends on iterator_traits , which itself depends on that particular language feature.

Example

Warnings

If your compiler does not support partial specialization, you can’t use the version of distance() that returns the distance. Instead, you must use the version that increments a reference parameter.

See Also

Standards Conformance

ISO/IEC 14882:1998 — International Standard for Information Systems — Programming Language C++, Section 24.3.4

std::distance

Поведение не определено, если last недоступен с first (возможно, многократно), увеличивая first .

Если InputIt не является LegacyRandomAccessIterator , поведение не определено, если last не достижимо с first (возможно, многократно), увеличивая first . Если InputIt равен LegacyRandomAccessIterator , поведение не определено, если last недостижим из first а first недостижим из last .

Parameters

first итератор,указывающий на первый элемент
last итератор,указывающий на конец диапазона
Type requirements
— InputIt должен соответствовать требованиям LegacyInputIterator . Операция более эффективна, если InputIt дополнительно отвечает требованиям LegacyRandomAccessIterator

Return value

Количество приращений, необходимое для перехода от first к last . Значение может быть отрицательным, если используются итераторы с произвольным доступом, и first доступен из last (начиная с C ++ 11).

Читать:
1с как объявить переменную

Complexity

Однако, если InputIt дополнительно отвечает требованиям LegacyRandomAccessIterator , сложность постоянна.

std:: distance

The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first .

If InputIt is not LegacyRandomAccessIterator , the behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first . If InputIt is LegacyRandomAccessIterator , the behavior is undefined if last is not reachable from first and first is not reachable from last .

Contents

[edit] Parameters

first iterator pointing to the first element
last iterator pointing to the end of the range
Type requirements

[edit] Return value

The number of increments needed to go from first to last . The value may be negative if random-access iterators are used and first is reachable from last (since C++11)

[edit] Complexity

However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator , complexity is constant.

Distance c что это

If we have two iterators and we want to find the total no. of elements between the two iterators, then that is facilitated by std::distance(), defined inside the header file .
It has an important feature that just like we have vectors in science, which have both magnitude and direction, std::distance also has direction associated with it. This means that calculating the distance between first and last and then calculating distance between last and first will not be same, as in the second case, it will have a negative sign associated with it, since we are moving backwards.
Syntax:

Похожие статьи