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/../ranges.html below:

Ranges library (since C++20) - cppreference.com

The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone.

The library creates and manipulates range views, lightweight objects that indirectly represent iterable sequences (ranges). Ranges are an abstraction on top of

The ranges library includes range algorithms, which are applied to ranges eagerly, and range adaptors, which are applied to views lazily. Adaptors can be composed into pipelines, so that their actions take place as the view is iterated.

namespace std {

    namespace views = ranges::views;

}
(since C++20)

The namespace alias std::views is provided as a shorthand for std::ranges::views.

[edit] Range factories [edit] Range adaptors [edit] Range generators (since C++23) [edit] Helper items [edit] Range adaptor objects

See RangeAdaptorObject (RAO).

[edit] Range adaptor closure objects

See RangeAdaptorClosureObject (RACO).

[edit] Customization point objects

See Customization point object (CPO).

[edit] Assignable wrapper

Some range adaptors wrap their elements or function objects with the copyable-box(until C++23)movable-box(since C++23). The wrapper augments the wrapped object with assignability when needed.

[edit] Non-propagating cache

Some range adaptors are specified in terms of an exposition-only class template non-propagating-cache, which behaves almost like std::optional<T> (see description for differences).

[edit] Conditionally-const type template< bool Const, class T >
using /*maybe-const*/ = std::conditional_t<Const, const T, T>;
(exposition only*)

The alias template /*maybe-const*/ is a shorthand used to conditionally apply a const qualifier to the type T.

[edit] Integer-like type helper templates

template< /*is-integer-like*/ T >
using /*make-signed-like-t*/<T> = /* see description */;

(1) (exposition only*)

template< /*is-integer-like*/ T >
using /*make-unsigned-like-t*/<T> = /* see description */;

(2) (exposition only*) template< /*is-integer-like*/ T >

/*make-unsigned-like-t*/<T> /*to-unsigned-like*/( T t )
{
    return static_cast</*make-unsigned-like-t*/<T>>(t);

}
(3) (exposition only*) 1)

For an

integer-like type T

:

2)

For an integer-like type

T

:

3) Explicitly converts t to /*make-unsigned-like-t*/<T>.

[edit] Customization point object helpers (1) (exposition only*) template< class T >

constexpr auto /*as-const-pointer*/( const T* p ) noexcept
{
    return p;

}
(2) (exposition only*)

Some range access customization point objects are specified in terms of these exposition-only function templates.

1) /*possibly-const-range*/

returns the const-qualified version of

r

if

const R

models

input_range

; otherwise, returns

r

without any casting.

2) /*as-const-pointer*/ returns a pointer to object of constant type.

[edit] Range adaptor helpers (1) (exposition only*) template< class F, class Tuple >

constexpr void /*tuple-for-each*/( F&& f, Tuple&& tuple )
{
    std::apply([&]<class... Ts>(Ts&&... args)
    {
        (static_cast<void>(std::invoke(f, std::forward<Ts>(args))), ...);
    }, std::forward<Tuple>(tuple));

}
(2) (exposition only*) template< class T >

constexpr T& /*as-lvalue*/( T&& t )
{
    return static_cast<T&>(t);

}
(3) (exposition only*)

Some range adaptors are specified in terms of these exposition-only function templates.

1) /*tuple-transform*/ returns a new tuple constructed by applying f to each element of tuple.

2) /*tuple-for-each*/ applies f to each element of tuple and returns nothing.

3) /*as-lvalue*/ forwards rvalue t as lvalue.

[edit] Helper concepts

Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.

(1) (exposition only*) template< class I >

concept /*has-arrow*/ =
    ranges::input_iterator<I> &&

    (std::is_pointer_v<I> || requires(const I i) { i.operator->(); });
(2) (exposition only*) (3) (exposition only*) (4) (exposition only*) (5) (exposition only*) (6) (exposition only*) (7) (exposition only*) [edit] Notes [edit] Example
#include <iostream>
#include <ranges>
 
int main()
{
    auto const ints = {0, 1, 2, 3, 4, 5};
    auto even = [](int i) { return 0 == i % 2; };
    auto square = [](int i) { return i * i; };
 
    // the "pipe" syntax of composing the views:
    for (int i : ints | std::views::filter(even) | std::views::transform(square))
        std::cout << i << ' ';
 
    std::cout << '\n';
 
    // a traditional "functional" composing syntax:
    for (int i : std::views::transform(std::views::filter(ints, even), square))
        std::cout << i << ' ';
}

Output:

[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 3509
(P2281R1) C++20 it was unclear how range adaptor objects bound trailing arguments they are bound
by value LWG 3948 C++23 possibly-const-range and as-const-pointer
were not declared noexcept declared noexcept LWG 4027 C++23 possibly-const-range would not add const-qualification
for ranges that has already modeled constant_range adds const-qualification
for such ranges LWG 4112 C++20 has-arrow did not require i to be const-qualified requires [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