Muokkaa

Jaa


transform_view class (C++ Standard Library)

A view of elements, each of which is a transformation of an element in the specified range.

Syntax

template<input_range V, move_constructible F>
    requires view<V> && is_object_v<F> &&
    regular_invocable<F&, range_reference_t<V>> &&
    can-reference<invoke_result_t<F&, range_reference_t<V>>>
class transform_view : public view_interface<transform_view<V, F>>;

Template parameters

F
The type of the function object that transforms the elements.

V
The type of the underlying view.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::transform
Underlying range Must satisfy input_range or higher
Element type Same as the transformation function's return type.
View iterator category Supports input_range up to random_access_range, depending on the underlying range
Sized Only if the underlying range satisfies sized_range
Is const-iterable Only if the underlying range is const iterable and the transformation works on const references.
Common range Only if the underlying range satisfies common_range
Borrowed range No

Members

Member functions Description
ConstructorsC++20 Construct the view.
baseC++20 Get the underlying range.
beginC++20 Get an iterator to the first element.
endC++20 Get the sentinel at the end of the view.
sizeC++20 Get the number of elements. The underlying range must satisfy sized_range.
Inherited from view_interface Description
backC++20 Get the last element.
emptyC++20 Test whether the view is empty.
frontC++20 Get the first element.
operator boolC++20 Test whether the view isn't empty.
operator[]C++20 Get the element at the specified position.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Construct an instance of a transform_view

1) transform_view() requires default_initializable<V>
         && default_initializable<F> = default;
2) constexpr transform_view(V base, F func);

Parameters

base
The underlying view.

func
The function that transforms each element.

For information about template parameter types, see Template parameters.

Return value

A transform_view instance.

Remarks

The best way to create a transform_view is by using the views::transform range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.

1) Create a value-initialized transform_view. The transformation function and the underlying view must be default-initializable.
2) Move construct the transform_view from a base view and a transformation function func. Both base and func are moved via std::move().

Example: transform_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
#include <chrono>

using namespace std;
using namespace chrono;

void print(auto v)
{
    for (auto x : v)
    {
        cout << x << ' ';
    }
    cout << '\n';
}

struct classes
{
    string className;
    weekday startDay;
};

int main()
{
    std::vector<int> v{0, 1, 2, 3, -4, 5, 6};

    // outputs 0 2 4 6 -8 10 12
    print(v | std::views::transform([](int i) {return i * 2; }));

    // ---- Modify the elements in the collection by returning a reference to the element to transform

    std::vector<classes> theClasses = {
        {"Math", Monday},
        {"English", Wednesday},
        {"History", Monday},
        {"Science", Wednesday},
        {"Art", Friday},
        {"Music", Thursday}
    };

    // lambda to get a reference to the day of the week for a class
    auto getDay = [](classes& c) -> weekday&
    {
        return c.startDay;
    };

    // If a class starts on Monday, change it to Tuesday
    for (auto&& startDay : theClasses | std::views::transform(getDay))
    {
        // modify the startDay in the collection
        if (startDay == Monday)
        {
            startDay = Tuesday;
        }
    }

    // output classes and start times
    for (auto c : theClasses)
    {
        std::cout << c.className << " : " << c.startDay << '\n';
    }
}
0 2 4 6 -8 10 12
Math : Tue
English : Wed
History : Tue
Science : Wed
Art : Fri
Music : Thu

base

Get the underlying view.

// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;

// Uses std::move() to return the underlying view
constexpr V base() &&;

Parameters

None.

Returns

The underlying view.

begin

Get an iterator to the first element in the view.

constexpr auto begin();

Return value

An iterator pointing at the first element in the view. The behavior is undefined if the view doesn't have a predicate.

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

end

Get the sentinel at the end of the view.

constexpr auto end()

Return value

The sentinel that follows the last element in the view:

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

size

Get the number of elements in the view.

constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;

Parameters

None.

Return value

The number of elements in the view.

See also

<ranges>
filter range adaptor
view classes