Difference between revisions of "cpp/ranges/join view"
From cppreference.com
m (→Top: add ';') |
m (add missing call signature synopsis) |
||
(17 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ranges/view title | join}} | + | {{cpp/ranges/view title|join}} |
{{cpp/ranges/join_view/navbar}} | {{cpp/ranges/join_view/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | ranges}} | + | {{dcl header|ranges}} |
− | {{dcl | num=1 | since=c++20 | | + | {{dcl|num=1|since=c++20| |
template< ranges::input_range V > | template< ranges::input_range V > | ||
− | + | requires ranges::view<V> and | |
− | + | ranges::input_range<ranges::range_reference_t<V>> | |
− | + | class join_view | |
− | class join_view : public ranges::view_interface<join_view<V>> | + | : public ranges::view_interface<join_view<V>> |
}} | }} | ||
− | {{dcl | num=2 | since=c++20 | 1= | + | {{dcl|num=2|since=c++20|1= |
namespace views { | namespace views { | ||
inline constexpr /* unspecified */ join = /* unspecified */; | inline constexpr /* unspecified */ join = /* unspecified */; | ||
} | } | ||
+ | }} | ||
+ | {{dcl h|Call signature}} | ||
+ | {{dcl|since=c++20|1= | ||
+ | template< ranges::viewable_range R > | ||
+ | requires /* see below */ | ||
+ | constexpr ranges::view auto join( R&& r ); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 20: | Line 26: | ||
@1@ A range adaptor that represents {{lconcept|view}} consisting of the sequence obtained from flattening a view of ranges. | @1@ A range adaptor that represents {{lconcept|view}} consisting of the sequence obtained from flattening a view of ranges. | ||
− | @2@ | + | @2@ {{named req|RangeAdaptorObject}} (and also {{named req|RangeAdaptorClosureObject}}). The expression {{c|views::join(e)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|join_view<views::all_t<decltype((e))>>{e}<!---->}} for any suitable subexpressions {{c|e}}. |
− | {{ | + | {{tt|join_view}} models {{lconcept|input_range}}. |
+ | |||
+ | {{tt|join_view}} models {{lconcept|forward_range}} when: | ||
+ | * {{c|ranges::range_reference_t<V>}} is a reference type, and | ||
+ | * {{c|V}} and {{c|ranges::range_reference_t<V>}} each model {{lconcept|forward_range}}. | ||
+ | |||
+ | {{tt|join_view}} models {{lconcept|bidirectional_range}} when: | ||
+ | * {{c|ranges::range_reference_t<V>}} is a reference type, | ||
+ | * {{c|V}} models {{lconcept|bidirectional_range}}, and | ||
+ | * {{c|ranges::range_reference_t<V>}} models both {{lconcept|bidirectional_range}} and {{lconcept|common_range}}. | ||
+ | |||
+ | {{tt|join_view}} models {{lconcept|common_range}} when: | ||
+ | * {{c|ranges::range_reference_t<V>}} is a reference type, and | ||
+ | * {{c|V}} and {{c|ranges::range_reference_t<V>}} each model {{lconcept|forward_range}} and {{lconcept|common_range}}. | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/ranges/adaptor/dsc constructor | join_view }} | + | {{dsc inc|cpp/ranges/adaptor/dsc constructor|join_view}} |
− | {{dsc inc | cpp/ranges/adaptor/dsc base | join_view }} | + | {{dsc inc|cpp/ranges/adaptor/dsc base|join_view}} |
− | {{dsc inc | cpp/ranges/adaptor/dsc begin | join_view }} | + | {{dsc inc|cpp/ranges/adaptor/dsc begin|join_view}} |
− | {{dsc inc | cpp/ranges/adaptor/dsc end | join_view }} | + | {{dsc inc|cpp/ranges/adaptor/dsc end|join_view}} |
+ | |||
+ | {{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|size=invalid|operator[]=invalid}} | ||
{{dsc end}} | {{dsc end}} | ||
Line 36: | Line 57: | ||
===Nested classes=== | ===Nested classes=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc mem | + | {{dsc expos mem tclass|cpp/ranges/join_view/iterator|the iterator type}} |
− | {{dsc mem | + | {{dsc expos mem tclass|cpp/ranges/join_view/sentinel|the sentinel type}} |
{{dsc end}} | {{dsc end}} | ||
===Notes=== | ===Notes=== | ||
− | + | Before {{wg21|P2328R1}} was adopted, the inner range type ({{c/core|ranges::range_reference_t<V>}}) cannot be a container type (but can be reference to container). For example, it was not allowed to join a {{rlpt|transform_view}} of {{lc|std::string}} prvalue. | |
{{source|1= | {{source|1= | ||
struct Person { int age; std::string name; }; | struct Person { int age; std::string name; }; | ||
+ | |||
auto f(std::vector<Person>& v) { | auto f(std::vector<Person>& v) { | ||
− | + | // return v {{!}} std::views::transform([](auto& p){ return p.name; }) | |
− | + | // {{!}} std::views::join; // error before P2328R1 | |
return v {{!}} std::views::transform([](auto& p) -> std::string& { return p.name; }) | return v {{!}} std::views::transform([](auto& p) -> std::string& { return p.name; }) | ||
{{!}} std::views::join; // OK | {{!}} std::views::join; // OK | ||
Line 64: | Line 86: | ||
{ | { | ||
using namespace std::literals; | using namespace std::literals; | ||
− | |||
− | |||
− | |||
− | const std::vector<std::vector<int>> v{ {1,2}, {3,4,5}, {6}, {7,8,9} }; | + | 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); | auto jv = std::ranges::join_view(v); | ||
− | for (int const e : jv) std::cout << e << ' '; | + | for (int const e : jv) |
+ | std::cout << e << ' '; | ||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
− | | output= | + | |output= |
https://cppreference.com | https://cppreference.com | ||
1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 7 8 9 | ||
Line 80: | Line 105: | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr=3474|std=C++20|before=views::join( | + | {{dr list item|wg=lwg|dr=3474|std=C++20|before={{c|views::join(e)}} returned a copy of {{c|e}} when {{c|e}} is a {{tt|join_view}}|after=returns a nested {{tt|join_view}}}} |
+ | {{dr list item|paper=P2328R1|std=C++20|before=non-view {{lconcept|range}} prvalues could not be joined by {{tt|join_view}}|after=made joinable}} | ||
{{dr list end}} | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/ranges/dsc | + | {{dsc inc|cpp/ranges/dsc join_with_view}} |
+ | {{dsc inc|cpp/ranges/dsc concat_view}} | ||
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 20:46, 6 August 2024
Defined in header <ranges>
|
||
template< ranges::input_range V > requires ranges::view<V> and |
(1) | (since C++20) |
namespace views { inline constexpr /* unspecified */ join = /* unspecified */; |
(2) | (since C++20) |
Call signature |
||
template< ranges::viewable_range R > requires /* see below */ |
(since C++20) | |
1) A range adaptor that represents
view
consisting of the sequence obtained from flattening a view of ranges.2) RangeAdaptorObject (and also RangeAdaptorClosureObject). The expression views::join(e) is expression-equivalent to join_view<views::all_t<decltype((e))>>{e} for any suitable subexpressions e.
join_view
models input_range
.
join_view
models forward_range
when:
- ranges::range_reference_t<V> is a reference type, and
- V and ranges::range_reference_t<V> each model
forward_range
.
join_view
models bidirectional_range
when:
- ranges::range_reference_t<V> is a reference type,
- V models
bidirectional_range
, and - ranges::range_reference_t<V> models both
bidirectional_range
andcommon_range
.
join_view
models common_range
when:
- ranges::range_reference_t<V> is a reference type, and
- V and ranges::range_reference_t<V> each model
forward_range
andcommon_range
.
Contents |
[edit] 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) | |
Inherited from std::ranges::view_interface | |
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range . (public member function of std::ranges::view_interface<D> )
| |
(C++23) |
returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D> )
|
(C++23) |
returns a sentinel for the constant iterator of the 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> )
| |
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
| |
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> )
|
[edit] Deduction guides
[edit] Nested classes
the iterator type (exposition-only member class template*) | |
the sentinel type (exposition-only member class template*) |
[edit] Notes
Before P2328R1 was adopted, the inner range type (ranges::range_reference_t<V>) cannot be a container type (but can be reference to container). For example, it was 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 before P2328R1 return v | std::views::transform([](auto& p) -> std::string& { return p.name; }) | std::views::join; // OK }
[edit] 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
[edit] 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) returned a copy of e when e is a join_view
|
returns a nested join_view
|
P2328R1 | C++20 | non-view range prvalues could not be joined by join_view
|
made joinable |
[edit] See also
a view consisting of the sequence obtained from flattening a view of ranges, with the delimiter in between elements(class template) (range adaptor object) | |
a view consisting of concatenation of the adapted views(class template) (customization point object) |