Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/take view"

From cppreference.com
< cpp‎ | ranges
m (rm last wrong semicolon; sorry..)
m (Example: fix: num => nums.)
 
(30 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{cpp/ranges/view title | take}}
+
{{cpp/ranges/view title|take}}
 
{{cpp/ranges/take_view/navbar}}
 
{{cpp/ranges/take_view/navbar}}
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | ranges}}
+
{{dcl header|ranges}}
{{dcl | num=1 | since=c++20 | 1=
+
{{dcl|num=1|since=c++20|1=
 
template< ranges::view V >
 
template< ranges::view V >
class take_view : public ranges::view_interface<take_view<V>>
+
class take_view
 +
    : public ranges::view_interface<take_view<V>>
 
}}
 
}}
{{dcl | num=2 | since=c++20 | 1=
+
{{dcl|num=2|since=c++20|1=
 
namespace views {
 
namespace views {
     inline constexpr /*unspecified*/ take = /*unspecified*/;
+
     inline constexpr /* unspecified */ take = /* unspecified */;
 
}
 
}
 
}}
 
}}
 
+
{{dcl h|Call signature}}
 +
{{dcl|since=c++20|1=
 +
template< ranges::viewable_range R >
 +
    requires /* see below */
 +
constexpr ranges::view auto
 +
    take( R&& r, ranges::range_difference_t<R> count );
 +
}}
 +
{{dcl|since=c++20|1=
 +
template< class DifferenceType >
 +
constexpr /* range adaptor closure */ take( DifferenceType&& count );
 +
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ A range adaptor that represents {{lconcept|view}} of the first ''N'' elements from an underlying sequence, or all elements if the underlying sequence contains fewer than ''N''.
+
@1@ A range adaptor that represents {{lconcept|view}} of the elements from an underlying sequence, starting at the beginning and ending at a given bound.
@2@ The expression {{c|views::take(E, F)}} results in a view that represents the first {{tt|F}} elements from {{tt|E}}. The result is not necessarily a {{tt|take_view}}.
+
@2@ {{tt|views::take}} is a {{named req|RangeAdaptorObject}}. The expression {{c|views::take(e, f)}} results in a view that represents the first {{c|f}} elements from {{c|e}}. The result is not necessarily a {{tt|take_view}}.
 
<!---->
 
<!---->
It is ''expression-equivalent'' to (where {{tt|T}} is {{c|std::remove_cvref_t<decltype((E))>}} and {{tt|D}} is {{c|ranges::range_difference_t<decltype((E))>}}):
+
{{c|views::take(e, f)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to (where {{tt|T}} is {{c/core|std::remove_cvref_t<decltype((e))>}} and {{tt|D}} is {{c/core|ranges::range_difference_t<decltype((e))>}}):
 
<!---->
 
<!---->
* {{c|((void)F, static_cast<T>(E))}}, if {{tt|T}} is a {{lc|ranges::empty_view}};
+
* {{box|{{c/core|((void)f,}}{{nbspt}}{{lti|cpp/standard library/decay-copy}}{{c/core|(e))}}}}, if {{tt|T}} is a {{c/core|ranges::empty_view}}, except that the evaluations of {{c|e}} and {{c|f}} are indeterminately sequenced;<!-- LWG 3524 -->
* {{c|T{ranges::begin(E), ranges::begin(E) + std::min<D>(ranges::size(E), F)} }}, if {{tt|T}} models both {{lconcept|random_access_range}} and {{lconcept|sized_range}}, and {{tt|T}} is a specialization of
+
* {{c|U(ranges::begin(e), ranges::begin(e) + std::min<D>(ranges::distance(e), f))}}, if {{tt|T}} is a specialization of {{c/core|std::span}}, {{lc|std::basic_string_view}}, or {{c/core|ranges::subrange}} that models both {{lconcept|random_access_range}} and {{lconcept|sized_range}}, where {{tt|U}} is
:* {{lc|std::span}} where {{tt|1=T::extent == std::dynamic_extent}},
+
:* {{c/core|std::span<typename T::element_type>}}, if {{tt|T}} is a specialization of {{c/core|std::span}};
:* {{lc|std::basic_string_view}},
+
:* {{tt|T}}, if {{tt|T}} is a specialization of {{lc|std::basic_string_view}};
:* {{lc|ranges::iota_view}}, or
+
:* {{c/core|ranges::subrange<ranges::iterator_t<T>>}}, if {{tt|T}} is a specialization of {{c/core|ranges::subrange}};
:* {{lc|ranges::subrange}};
+
* {{c multi|ranges::iota_view(*ranges::begin(e),|                  *(ranges::begin(e) + std::min<D>(ranges::distance(e), f)))}}, if {{tt|T}} is a specialization of {{c/core|ranges::iota_view}} that models both {{lconcept|random_access_range}} and {{lconcept|sized_range}};
* otherwise, {{c|take_view{E, F} }}.
+
{{rrev|since=c++23|
 +
* otherwise, if {{tt|T}} is a specialization of {{l2tt|cpp/ranges/repeat_view}}:
 +
:* {{c|views::repeat(*e.value_, std::min<D>(ranges::distance(e), f))}}, if {{tt|T}} models {{lconcept|sized_range}}; is such case {{c|e}} is evaluated only once;
 +
:* {{c|views::repeat(*e.value_, static_cast<D>(e))}} otherwise;
 +
}}
 +
* otherwise, {{c|take_view(e, f)}}.
 +
<!---->
 +
In all cases, {{c/core|decltype((f))}} must model {{c|std::convertible_to<D>}}.
  
In all cases, {{tt|decltype((F))}} must model {{tt|std::convertible_to<D>}}.
+
{{tt|take_view}} models the concepts {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}},  {{lconcept|input_range}}, and {{lconcept|sized_range}} when the underlying view {{tt|V}} models respective concepts. It models {{lconcept|common_range}} when the underlying view {{tt|V}} models both {{lconcept|random_access_range}} and {{lconcept|sized_range}}.
  
<!--
+
===Data members===
{{c|take_view}} models the concepts {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}},  {{lconcept|input_range}}, and {{lconcept|sized_range}} when the underlying view {{c|V}} models respective concepts. It models {{lconcept|common_range}} when the underlying view {{c|V}} models both {{lconcept|random_access_range}} and {{lconcept|sized_range}}.
+
{{dsc begin}}
-->
+
{{dsc hitem|Member name|Definition}}
 
+
{{dsc expos mem obj|base_|private=yes|the underlying {{lconcept|view}} of type {{tt|V}}}}
{{cpp/expr-eq}}
+
{{dsc expos mem obj|count_|private=yes|the number of elements to take, of type {{c/core|ranges::range_difference_t<V>}}}}
 +
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/adaptor/dsc constructor | take_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc constructor|take_view}}
{{dsc inc | cpp/ranges/adaptor/dsc base | take_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc base|take_view}}
{{dsc inc | cpp/ranges/adaptor/dsc begin | take_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc begin|take_view}}
{{dsc inc | cpp/ranges/adaptor/dsc end | take_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc end|take_view}}
{{dsc inc | cpp/ranges/adaptor/dsc size | take_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc size|take_view}}
 +
 
 +
{{cpp/ranges/view_interface/inherit|embedded=yes|size=yes}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 50: Line 71:
 
===Nested classes===
 
===Nested classes===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem class| cpp/ranges/take_view/sentinel | title=''sentinel'' | the sentinel type}}  
+
{{dsc expos mem tclass|cpp/ranges/take_view/sentinel|notes={{mark c++20}}|the sentinel type}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Helper templates===
 
===Helper templates===
 
{{ddcl|since=c++20|1=
 
{{ddcl|since=c++20|1=
template<class T>
+
template< class T >
inline constexpr bool enable_borrowed_range<std::ranges::take_view<T>> =
+
constexpr bool enable_borrowed_range<std::ranges::take_view<T>> =
     std::ranges::enable_borrowed_range<T>;  
+
     ranges::enable_borrowed_range<T>;
 
}}
 
}}
 
This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|take_view}} satisfy {{lconcept|borrowed_range}} when the underlying view satisfies it.
 
This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|take_view}} satisfy {{lconcept|borrowed_range}} when the underlying view satisfies it.
Line 67: Line 88:
 
#include <iostream>
 
#include <iostream>
 
#include <ranges>
 
#include <ranges>
 
auto print = [](char x) { std::cout << x; };
 
  
 
int main()
 
int main()
 
{
 
{
     constexpr char pi[] { '3', '.', '1', '4', '1', '5', '9', '2' };
+
     namespace views = std::views;
 
+
     auto print = [](char x){ std::cout << x; };
    std::ranges::for_each(pi {{!}} std::ranges::views::take(6), print);
+
     std::cout << '\n';
+
  
     std::ranges::for_each(std::ranges::take_view{pi, 42}, print); // safely takes only 8 chars
+
     for (const char nums[]{'1', '2', '3'};
    std::cout << '\n';
+
        int n : views::iota(0, 5))
 +
    {
 +
        std::cout << "take(" << n << "): ";
 +
        // safely takes only upto min(n, nums.size()) elements:
 +
        std::ranges::for_each(nums {{!}} views::take(n), print);
 +
        std::cout << '\n';
 +
    }
 
}
 
}
|output=
+
|output=
3.1415
+
take(0):
3.141592
+
take(1): 1
 +
take(2): 12
 +
take(3): 123
 +
take(4): 123
 
}}
 
}}
  
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
{{dr list item|wg=lwg|dr=3494|std=c++20|before={{tt|take_view}} was never a {{tt|borrowed_range}}|after=it is a {{tt|borrowed_range}} if its underlying view is}}
+
{{dr list item|wg=lwg|dr=3407|std=C++20|before={{tt|views::take}} sometimes failed to<br>construct a sized random access range|after=the result type is adjusted so<br>that construction is always valid}}
 +
{{dr list item|wg=lwg|dr=3494|std=C++20|before={{tt|take_view}} was never a {{tt|borrowed_range}}|after=it is a {{tt|borrowed_range}} if its underlying view is}}
 
{{dr list end}}
 
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/dsc view_counted}}
+
{{dsc inc|cpp/ranges/dsc view_counted}}
{{dsc inc | cpp/ranges/dsc take_while_view}}
+
{{dsc inc|cpp/ranges/dsc take_while_view}}
{{dsc inc | cpp/algorithm/ranges/dsc copy_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc copy_n}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|ru|zh}}
 
{{langlinks|es|ja|ru|zh}}

Latest revision as of 13:21, 14 August 2024

 
 
Ranges library
Range adaptors
 
 
Defined in header <ranges>
template< ranges::view V >

class take_view

    : public ranges::view_interface<take_view<V>>
(1) (since C++20)
namespace views {

    inline constexpr /* unspecified */ take = /* unspecified */;

}
(2) (since C++20)
Call signature
template< ranges::viewable_range R >

    requires /* see below */
constexpr ranges::view auto

    take( R&& r, ranges::range_difference_t<R> count );
(since C++20)
template< class DifferenceType >
constexpr /* range adaptor closure */ take( DifferenceType&& count );
(since C++20)
1) A range adaptor that represents view of the elements from an underlying sequence, starting at the beginning and ending at a given bound.
2) views::take is a RangeAdaptorObject. The expression views::take(e, f) results in a view that represents the first f elements from e. The result is not necessarily a take_view.

views::take(e, f) is expression-equivalent to (where T is std::remove_cvref_t<decltype((e))> and D is ranges::range_difference_t<decltype((e))>):

(since C++23)
  • otherwise, take_view(e, f).
In all cases, decltype((f)) must model std::convertible_to<D>.

take_view models the concepts contiguous_range, random_access_range, bidirectional_range, forward_range, input_range, and sized_range when the underlying view V models respective concepts. It models common_range when the underlying view V models both random_access_range and sized_range.

Contents

[edit] Data members

Member name Definition
base_ (private) the underlying view of type V
(exposition-only member object*)
count_ (private) the number of elements to take, of type ranges::range_difference_t<V>
(exposition-only member object*)

[edit] Member functions

constructs a take_view
(public member function) [edit]
returns a copy of the underlying (adapted) view
(public member function) [edit]
returns an iterator to the beginning
(public member function) [edit]
returns an iterator or a sentinel to the end
(public member function) [edit]
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range.
(public member function) [edit]
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>) [edit]
(C++23)
returns a constant iterator to the beginning of the range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a sentinel for the constant iterator of the range.
(public member function of std::ranges::view_interface<D>) [edit]
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>) [edit]
gets the address of derived view's data. Provided if its iterator type satisfies contiguous_iterator.
(public member function of std::ranges::view_interface<D>) [edit]
returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
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]
returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

[edit] Deduction guides

[edit] Nested classes

(C++20)
the sentinel type
(exposition-only member class template*)

[edit] Helper templates

template< class T >

constexpr bool enable_borrowed_range<std::ranges::take_view<T>> =

    ranges::enable_borrowed_range<T>;
(since C++20)

This specialization of std::ranges::enable_borrowed_range makes take_view satisfy borrowed_range when the underlying view satisfies it.

[edit] Example

#include <algorithm>
#include <iostream>
#include <ranges>
 
int main()
{
    namespace views = std::views;
    auto print = [](char x){ std::cout << x; };
 
    for (const char nums[]{'1', '2', '3'};
         int n : views::iota(0, 5))
    {
        std::cout << "take(" << n << "): ";
        // safely takes only upto min(n, nums.size()) elements:
        std::ranges::for_each(nums | views::take(n), print);
        std::cout << '\n';
    }
}

Output:

take(0): 
take(1): 1
take(2): 12
take(3): 123
take(4): 123

[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 3407 C++20 views::take sometimes failed to
construct a sized random access range
the result type is adjusted so
that construction is always valid
LWG 3494 C++20 take_view was never a borrowed_range it is a borrowed_range if its underlying view is

[edit] See also

creates a subrange from an iterator and a count
(customization point object)[edit]
a view consisting of the initial elements of another view, until the first element on which a predicate returns false
(class template) (range adaptor object)[edit]
copies a number of elements to a new location
(niebloid)[edit]