이름공간
변수
행위

Ranges library (C++20)

cppreference.com
< cpp
 
 
 

The ranges library provides components for dealing with ranges of elements, including a variety of view adaptors.

<ranges> 에 정의되어 있음.
namespace std {

    namespace views = ranges::views;

}
(since C++20)

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

틀:cpp/ranges/dsc cbegin틀:cpp/ranges/dsc cend틀:cpp/ranges/dsc rbegin틀:cpp/ranges/dsc rend틀:cpp/ranges/dsc crbegin틀:cpp/ranges/dsc crend틀:cpp/ranges/dsc size틀:cpp/ranges/dsc ssize틀:cpp/ranges/dsc empty틀:cpp/ranges/dsc data틀:cpp/ranges/dsc cdata틀:cpp/ranges/dsc iterator t틀:cpp/ranges/dsc dangling틀:cpp/ranges/dsc borrowed iterator t틀:cpp/ranges/dsc range틀:cpp/ranges/dsc borrowed range틀:cpp/ranges/dsc sized range틀:cpp/ranges/dsc view틀:cpp/ranges/dsc input range틀:cpp/ranges/dsc output range틀:cpp/ranges/dsc forward range틀:cpp/ranges/dsc bidirectional range틀:cpp/ranges/dsc random access range틀:cpp/ranges/dsc contiguous range틀:cpp/ranges/dsc common range틀:cpp/ranges/dsc viewable range틀:cpp/ranges/dsc view interface
Defined in namespace std::ranges

목차

Range access
<ranges> 헤더에 정의됨.
<iterator> 헤더에 정의됨.
범위(range)의 시작점을 가리키는 반복자를 반환합니다
(customization point object) [edit]
범위의 끝 경계값을 반환합니다
(customization point object) [edit]
Range primitives
<ranges> 헤더에 정의됨.
Dangling iterator handling
<ranges> 헤더에 정의됨.
Range concepts
<ranges> 헤더에 정의됨.
Views
<ranges> 헤더에 정의됨.
combines an iterator-sentinel pair into a view
(class template) [edit]

[편집] Range factories

틀:cpp/ranges/dsc empty view틀:cpp/ranges/dsc single view틀:cpp/ranges/dsc iota view틀:cpp/ranges/dsc basic istream view
<ranges> 헤더에 정의됨.
Defined in namespace std::ranges

[편집] Range adaptors

틀:cpp/ranges/dsc all view틀:cpp/ranges/dsc ref view틀:cpp/ranges/dsc owning view틀:cpp/ranges/dsc filter view틀:cpp/ranges/dsc transform view틀:cpp/ranges/dsc take view틀:cpp/ranges/dsc take while view틀:cpp/ranges/dsc drop view틀:cpp/ranges/dsc drop while view틀:cpp/ranges/dsc join view틀:cpp/ranges/dsc split view틀:cpp/ranges/dsc lazy split view틀:cpp/ranges/dsc view counted틀:cpp/ranges/dsc common view틀:cpp/ranges/dsc reverse view틀:cpp/ranges/dsc elements view틀:cpp/ranges/dsc keys view틀:cpp/ranges/dsc values view틀:cpp/ranges/dsc zip view틀:cpp/ranges/dsc zip transform view틀:cpp/ranges/dsc adjacent view틀:cpp/ranges/dsc adjacent transform view
<ranges> 헤더에 정의됨.
Defined in namespace std::ranges

Some range adaptors wrap their elements or function objects with the copyable wrapper.

[편집] Range adaptor objects

Range adaptor objects are customization point objects that accept viewable_range as their first arguments and return a view. Some range adaptor objects are unary, i.e. they takes one viewable_range as its only argument. Other range adaptor objects takes a viewable_range and other trailing arguments.

If a range adaptor object takes more than one argument, it also supports partial application: let

  • a be such a range adaptor object, and
  • args... be arguments (generally suitable for trailing arguments),

expression a(args...) has following properties:

  • it is valid if and only if for every argument e in args... such that E is decltype((e)), std::is_constructible_v<std::decay_t<E>, E> is true,
  • when the call is valid, its result object stores a subobject of type std::decay_t<E> direct-non-list-initialized with std::forward<E>(e), for every argument e in args... (in other words, range adaptor objects bind arguments by value), and
  • the result object is a range adaptor closure object.

Like other customization point objects, let

  • a be an object of the cv-unqualified version of the type of any range adaptor objects,
  • args... be any group of arguments that satisfies the constraints of the operator() of the type of a,

calls to

  • a(args...),
  • std::as_const(a)(args...),
  • std::move(a)(args...), and
  • std::move(std::as_const(a))(args...)

are all equivalent.

The result object of each of these expressions is either a view object or a range adaptor closure object.

Notes: operator() is unsupported for volatile-qualified or const-volatile-qualified version of range adaptor object types. Arrays and functions are converted to pointers while binding.

[편집] Range adaptor closure objects

Range adaptor closure objects are objects whose type is the same as one of the following objects (ignoring cv-qualification):

  • unary range adaptor objects,
  • the results of binding trailing arguments by range adaptor objects, and
  • the results of chaining two range adaptor closure objects by operator|.

Range adaptor closure objects take one viewable_range as its only argument and return a view. They are callable via the pipe operator: if C is a range adaptor closure object and R is a viewable_range, these two expressions are equivalent (both well-formed or ill-formed):

C(R)
R | C

This call forwards the bound arguments (if any) to the associated range adaptor object. The bound arguments (if any) are identically treated as lvalue or rvalue and cv-qualified to C.

Two range adaptor closure objects can be chained by operator| to produce another range adaptor closure object: if C and D are range adaptor closure objects, then C | D is also a range adaptor closure object if it is valid.

The bound arguments of C | D is determined as follows:

  • there is a subobject in the result object of the same type (cv-qualification discarded) for every subobject in both operands that is a bound argument,
  • such a bound argument is direct-non-list-initialized with the source subobject in its containing operand, where the source is identically treated as lvalue or rvalue and cv-qualified to the operand,
  • the result is valid if and only if the initialization of all bound arguments are valid.

The effect and validity of the operator() of the result is determined as follows: given a viewable_range R, these two expressions are equivalent (both well-formed or ill-formed):

R | C | D // (R | C) | D
R | (C | D)

Notes: operator() is unsupported for volatile-qualified or const-volatile-qualified version of range adaptor object closure types.

[편집] Helper concepts

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

template<class R>

  concept __SimpleView =                         // exposition only
    ranges::view<R> && ranges::range<const R> &&
    std::same_as<std::ranges::iterator_t<R>, std::ranges::iterator_t<const R>> &&

    std::same_as<std::ranges::sentinel_t<R>, std::ranges::sentinel_t<const R>>;

[편집] Example

#include <ranges>
#include <iostream>
 
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; };
 
    // "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:

0 4 16
0 4 16

[편집] 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 C++20 it was unclear how range adaptor objects bound trailing arguments they are bound by value