std::ranges...ansform_view(3)
π std::ranges::views::transform & std::ranges::transform_view π
okay so bestie, this is the C++ Standard Libary manual page std::ranges...ansform_view(3) β yes, the title is a little brat with those dots, but it's iconic. we're covering both std::ranges::views::transform and std::ranges::transform_view, and honestly? they ate. π
β¨
β¨ NAME β¨
std::ranges::views::transform, std::ranges::transform_view β literally the whole name section, bestie. two names, one vibe, same page. π
πΈ Synopsis πΈ
Defined in header <ranges> β don't forget the header, or the compiler will be like "girl what". π
here's the first class template situation, since C++20 and until C++23, exact:
template< ranges::input_range V,
std::copy_constructible F >
requires ranges::view<V> &&
std::is_object_v<F> &&
std::regular_invocable<F&,
(since C++20)
ranges::range_reference_t<V>> &&
(until C++23)
/* invoke_result_t<F&, range_reference_t<V>>& is
a valid type */
class transform_view
: public ranges::view_interface<transform_view<V, F>>
and here's the second one, since C++23, with its (1) marker and everything:
template< ranges::input_range V,
std::move_constructible F >
requires ranges::view<V> &&
std::is_object_v<F> &&
std::regular_invocable<F&,
(since C++23)
ranges::range_reference_t<V>> && (1)
/* invoke_result_t<F&, range_reference_t<V>>& is
a valid type */
class transform_view
: public ranges::view_interface<transform_view<V, F>>
and the views namespace bit, since C++20:
namespace views {
inline constexpr /*unspecified*/ transform =
(2) (since C++20)
/*unspecified*/;
}
π Call signature π
since C++20, bestie:
template< ranges::viewable_range R, class F >
requires /* see below */
(since C++20)
constexpr ranges::view auto transform( R&& r, F&& fun );
template< class F >
(since C++20)
constexpr /*range adaptor closure*/ transform( F&& fun );
okay so let's decode the lore:
transform_viewis a range adaptor that represents a view of an underlying sequence after applying a transformation function to each element. it's giving glow-up for every element. π¦views::transformis aRangeAdaptorObject. the expressionviews::transform(e, f)is expression-equivalent totransform_view(e, f)for any suitable subexpressionseandf. no cap, it's just the cute wrapper. π
and the vibe check: 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. (yes, respective β don't @ me.) π
π«Ά Member functions π«Ά
constructor(C++20) β constructs atransform_view(public member function).base(C++20) β returns a copy of the underlying (adapted) view (public member function).begin(C++20) β returns an iterator to the beginning (public member function).end(C++20) β returns an iterator or a sentinel to the end (public member function).size(C++20) β returns the number of elements. Provided only if the underlying (adapted) range satisfiessized_range. (public member function)
Inherited from std::ranges::view_interface
empty(C++20) β returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (public member function ofstd::ranges::view_interface<D>)cbegin(C++23) β returns a constant iterator to the beginning of the range. (public member function ofstd::ranges::view_interface<D>)cend(C++23) β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20) β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)front(C++20) β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20) β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::ranges::view_interface<D>)operator[](C++20) β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range. (public member function ofstd::ranges::view_interface<D>)
π Deduction guides π
bestie, this page just says "Deduction guides" and then moves on. no extra bullets, no secret sauce listed here. π
π¦ Nested classes π¦
iteratorβ the iterator type. (C++20) (exposition-only member class template*)sentinelβ the sentinel type. (C++20) (exposition-only member class template*)
π Example π
// Run this code
#include <algorithm>
#include <cstdio>
#include <iterator>
#include <ranges>
#include <string>
char rot13a(const char x, const char a)
{
return a + (x - a + 13) % 26;
}
char rot13(const char x)
{
if ('Z' >= x and x >= 'A')
return rot13a(x, 'A');
if ('z' >= x and x >= 'a')
return rot13a(x, 'a');
return x;
}
int main()
{
auto show = [](const unsigned char x) { std::putchar(x); };
std::string in{"cppreference.com\n"};
std::ranges::for_each(in, show);
std::ranges::for_each(in | std::views::transform(rot13), show);
std::string out;
std::ranges::copy(std::views::transform(in, rot13),
std::back_inserter(out));
std::ranges::for_each(out, show);
std::ranges::for_each(out | std::views::transform(rot13), show);
}
π Output π
cppreference.com
pccersrerapr.pbz
pccersrerapr.pbz
cppreference.com
π See also π
ranges::transformβ applies a function to a range of elements. (C++20) (niebloid). π«
okay that's the whole page, bestie. source: http://cppreference.com, version 2024.06.10, page std::ranges...ansform_view(3) β now go transform something and slay. πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms