A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../named_req/../iterator/ranges/distance.html below:

std::ranges::distance - cppreference.com

1,2) Returns the number of hops from first to last.

3) Returns the size of r as a signed integer.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

[edit] Parameters first - iterator pointing to the first element last - sentinel denoting the end of the range first is an iterator to r - range to calculate the distance of [edit] Return value

1) The number of increments needed to go from first to last.

[edit] Complexity

1) Linear.

2) Constant.

[edit] Possible implementation [edit] Example
#include <cassert>
#include <forward_list>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{3, 1, 4};
    assert(std::ranges::distance(v.begin(), v.end()) == 3);
    assert(std::ranges::distance(v.end(), v.begin()) == -3);
    assert(std::ranges::distance(v) == 3);
 
    std::forward_list<int> l{2, 7, 1};
    // auto size = std::ranges::size(l); // error: not a sizable range
    auto size = std::ranges::distance(l); // OK, but aware O(N) complexity
    assert(size == 3);
}
[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior LWG 3392 C++20 overload (1) takes iterator by value, thus move-only
iterator lvalue with a sized sentinel was rejected added overload (2) LWG 3664 C++20 the resolution of LWG issue 3392 made
ranges::distance reject array arguments accepts them [edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4