Difference between revisions of "cpp/ranges/transform view"
From cppreference.com
m |
(link to cpp/ranges#Range_adaptors) |
||
Line 22: | Line 22: | ||
@1@ A range adaptor that represents {{lconcept|view}} of an underlying sequence after applying a transformation function to each element. | @1@ A range adaptor that represents {{lconcept|view}} of an underlying sequence after applying a transformation function to each element. | ||
− | @2@ The expression {{c|views::transform(E, F)}} is ''expression-equivalent'' to {{c|transform_view{E, F} }} for any suitable subexpressions {{c|E}} and {{c|F}}. | + | @2@ [[cpp/ranges#Range adaptors|Range adaptor object]]. The expression {{c|views::transform(E, F)}} is ''expression-equivalent'' to {{c|transform_view{E, F} }} for any suitable subexpressions {{c|E}} and {{c|F}}. |
{{c|transform_view}} models the concepts {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}}, {{lconcept|input_range}}, {{lconcept|common_range}}, and {{lconcept|sized_range}} when the underlying view {{c|V}} models respective concepts. | {{c|transform_view}} models the concepts {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}}, {{lconcept|input_range}}, {{lconcept|common_range}}, and {{lconcept|sized_range}} when the underlying view {{c|V}} models respective concepts. |
Revision as of 20:42, 29 July 2020
Defined in header <ranges>
|
||
template< ranges::input_range V, std::copy_constructible F > |
(1) | (since C++20) |
namespace views { inline constexpr /*unspecified*/ transform = /*unspecified*/; |
(2) | (since C++20) |
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.
Contents |
Expression-equivalent
Expression e is expression-equivalent to expression f, if
- e and f have the same effects, and
- either both are constant subexpressions or else neither is a constant subexpression, and
- either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).
Member functions
constructs a transform_view (public member function) | |
returns a copy of the underlying (adapted) view (public member function) | |
returns an iterator to the beginning (public member function) | |
returns an iterator or a sentinel to the end (public member function) | |
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range . (public member function) |
Deduction guides
Nested classes
the iterator type (public member class) | |
the sentinel type (public member class) |
Example
Run this code
#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::ranges::back_inserter(out)); std::ranges::for_each(out, show); auto view = std::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
(C++20) |
applies a function to a range of elements (niebloid) |