std::ranges::views::transform, std::ranges::transform_view

From cppreference.com
< cpp‎ | ranges
 
 
 
 
Defined in header <ranges>
template< ranges::input_range V,

          std::copy_constructible F >
  requires ranges::view<V> &&
           std::is_object_v<F> &&
           std::regular_invocable<F&, ranges::range_reference_t<V>> &&
           /* invoke_result_t<F&, range_reference_t<V>>& is a valid type */

class transform_view : public ranges::view_interface<transform_view<V, F>>
(1) (since C++20)
namespace views {

    inline constexpr /*unspecified*/ transform = /*unspecified*/;

}
(2) (since C++20)
Call signature
template< ranges::viewable_range R, class F >

    requires /* see below */

constexpr ranges::view auto transform( R&& r, F&& fun );
template< class F >
constexpr /*range adaptor closure*/ transform( F&& fun );
1) A range adaptor that represents view of an underlying sequence after applying a transformation function to each element.
2) Range adaptor object. The expression views::transform(e, f) is expression-equivalent to transform_view(e, f) for any suitable subexpressions e and f.

transform_view models the concepts random_access_range, bidirectional_range, forward_range, input_range, common_range, and sized_range when the underlying view V models respective concepts.

Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

Member functions

constructs a transform_view
(public member function)
(C++20)
returns a copy of the underlying (adapted) view
(public member function)
(C++20)
returns an iterator to the beginning
(public member function)
(C++20)
returns an iterator or a sentinel to the end
(public member function)
(C++20)
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range
(public member function)
Inherited from std::ranges::view_interface
(C++20)
Returns whether the derived view is empty. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>)
Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>)
(C++20)
Returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>)
(C++20)
Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>)
Returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>)

Deduction guides

Nested classes

(C++20)
the iterator type
(exposition-only member class template)
(C++20)
the sentinel type
(exposition-only member class template)

Example

#include <algorithm>
#include <array>
#include <cstdio>
#include <ranges>
#include <string>
 
class Transcoder {
    static constexpr std::array r{
        0x02,-0x02,-0x42, 0x05, 0x04, 0x05, 0x04,-0x02,
        0x00,-0x0a, 0x06,-0x04, 0x00, 0x0c, 0x03,-0x06,
    };
    decltype(r.size()) p{}, q{};
public:
    char operator() (int x) {
        ! (p|q)
        ? (99 == x ? x += r[p++]  : x -= r[q++])
        : (0 < p && p < r.size()) ? x += r[p++]
        : (0 < q && q < r.size()) ? x -= r[q++]
        : (0)
        ;
        return x;
    }
};
 
int main()
{
    auto show = [](const char x) { std::putchar(x); };
 
    std::string in{ "cppreference.com\n" };
    std::ranges::for_each(in, show);
 
    std::string out;
    std::ranges::copy(
        std::ranges::views::transform(in, Transcoder{}),
        std::back_inserter(out));
    std::ranges::for_each(out, show);
 
    auto view = std::ranges::transform_view{ out, Transcoder{} };
    std::ranges::for_each(view, show);
    std::ranges::for_each(view, show);
}

Output:

cppreference.com
en.wikipedia.org
cppreference.com
en.wikipedia.org

See also

applies a function to a range of elements
(niebloid)