Difference between revisions of "cpp/ranges/join view"
From cppreference.com
D41D8CD98F (Talk | contribs) |
m (→Top: add ';') |
||
Line 9: | Line 9: | ||
(std::is_reference_v<ranges::range_reference_t<V>> {{!!}} | (std::is_reference_v<ranges::range_reference_t<V>> {{!!}} | ||
ranges::view<ranges::range_value_t<V>>) | ranges::view<ranges::range_value_t<V>>) | ||
− | class join_view : public ranges::view_interface<join_view<V>> | + | class join_view : public ranges::view_interface<join_view<V>>; |
}} | }} | ||
{{dcl | num=2 | since=c++20 | 1= | {{dcl | num=2 | since=c++20 | 1= |
Revision as of 14:15, 10 May 2021
Defined in header <ranges>
|
||
template< ranges::input_range V > requires ranges::view<V> && ranges::input_range<ranges::range_reference_t<V>> && |
(1) | (since C++20) |
namespace views { inline constexpr /* unspecified */ join = /* unspecified */; |
(2) | (since C++20) |
1) A range adaptor that represents
view
consisting of the sequence obtained from flattening a view of ranges.2) Range adaptor object. The expression views::join(E) is expression-equivalent to join_view<views::all_t<decltype((E))>>{E} for any suitable subexpressions E.
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 join_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) |
Deduction guides
Nested classes
the iterator type (public member class) | |
the sentinel type (public member class) |
Notes
The inner range type (ranges::range_reference_t<V>) cannot be a container type (but can be reference to container). For example, it's not allowed to join a transform_view
of std::string prvalue.
struct Person { int age; std::string name; }; auto f(std::vector<Person>& v) { // return v | std::views::transform([](auto& p) { return p.name; }) // | std::views::join; // error return v | std::views::transform([](auto& p) -> std::string& { return p.name; }) | std::views::join; // OK }
Example
Run this code
#include <iostream> #include <ranges> #include <string_view> #include <vector> int main() { using namespace std::literals; const auto bits = { "https:"sv, "//"sv, "cppreference"sv, "."sv, "com"sv }; for (char const c : bits | std::views::join) std::cout << c; std::cout << '\n'; const std::vector<std::vector<int>> v{ {1,2}, {3,4,5}, {6}, {7,8,9} }; auto jv = std::ranges::join_view(v); for (int const e : jv) std::cout << e << ' '; std::cout << '\n'; }
Output:
https://cppreference.com 1 2 3 4 5 6 7 8 9
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 3474 | C++20 | views::join(E) returns a copy of E when E is a join_view | returns a nested join_view |
See also
a view over the subranges obtained from splitting another view using a delimiter(class template) (range adaptor object) |