std::ranges...p_while_view(3)
π std::ranges...p_while_view(3) π
okay so welcome to the std::ranges...p_while_view(3) page from the C++ Standard Libary manual. it's giving range-adaptor realness, and it's literally all about std::ranges::views::drop_while and std::ranges::drop_while_view. bestie, buckle up π
πΈ NAME πΈ
std::ranges::views::drop_while,std::ranges::drop_while_view β std::ranges::views::drop_while,std::ranges::drop_while_view. that's the whole NAME section, no cap β¨
π¦ Synopsis π¦
Defined in header <ranges>.
template< ranges::view V, class Pred >
requires ranges::input_range<V> &&
std::is_object_v<Pred> &&
std::indirect_unary_predicate<const Pred,
ranges::iterator_t<V>>
class drop_while_view
: public ranges::view_interface<drop_while_view<V, Pred>>
(1) (since C++20) β this is the class template energy, and the (1) marker in the source belongs right here. π
namespace views {
inline constexpr /* unspecified */ drop_while = /* unspecified */;
}
(2) (since C++20) β this is the range adaptor object, and the (2) marker belongs to it. π¦
π Call signature π
template< ranges::viewable_range R, class Pred >
requires /* see below */
constexpr ranges::view auto drop_while( R&& r, Pred&& pred );
(since C++20)
template< class Pred >
constexpr /*range adaptor closure*/ drop_while( Pred&& pred );
(since C++20)
- A range adaptor that represents view of elements from an underlying sequence, beginning at the first element for which the predicate returns false. so it's literally "skip the stuff while the predicate says yes, then start here." πΈ
RangeAdaptorObject. The expressionviews::drop_while(e, f)is expression-equivalent todrop_while_view(e, f)for any suitable subexpressionseandf. β¨
drop_while_view models the concepts contiguous_range, random_access_range, bidirectional_range, forward_range, input_range, and common_range when the underlying view V models respective concepts. It also models sized_range if ranges::forward_range<V> and std::sized_sentinel_for<ranges::sentinel_t<V>, ranges::iterator_t<V>> are modeled. that's a whole lot of concepts, and yes, it's iconic. π
π Member functions π
okay so here's the whole menu, bestie:
constructor(C++20) β constructs adrop_while_view(public member function)base(C++20) β returns a copy of the underlying (adapted) view (public member function)pred(C++20) β returns a reference to the stored predicate (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)
π 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>)data(C++20) β gets the address of derived view's data. Provided if its iterator type satisfiescontiguous_iterator. (public member function ofstd::ranges::view_interface<D>)size(C++20) β returns the number of elements in the derived view. Provided if it satisfiesforward_rangeand its sentinel and iterator type satisfysized_sentinel_for. (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 π
literally just the heading in the source, no extra deets listed. periodt π
π§ Helper templates π§
template< class T, class Pred >
inline constexpr bool enable_borrowed_range<std::ranges::drop_while_view<T, Pred>> =
std::ranges::enable_borrowed_range<T>;
(since C++20)
This specialization of std::ranges::enable_borrowed_range makes drop_while_view satisfy borrowed_range when the underlying view satisfies it. cute, borrowed, slay. π«Ά
π Notes π
bestie warning: In order to provide the amortized constant time complexity required by the range concept, the result ofbeginis cached within thedrop_while_viewobject. If the underlying range is modified after the first call tobegin(), subsequent uses of thedrop_while_viewobject might have unintuitive behavior. π so don't go mutating the underlying range and then acting surprised, okay?
π Example π
okay so here's the example, copied exactly because we do not mess with code around here π
// Run this code
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
using std::operator""sv;
[[nodiscard]]
constexpr bool is_space(char q) noexcept
{
static constexpr auto ws = {' ', '\t', '\n', '\v', '\r', '\f'};
return std::ranges::any_of(ws, [q](auto p) { return p == q; });
};
[[nodiscard]]
constexpr std::string trim_left(std::string_view const in) noexcept
{
auto view = in | std::views::drop_while(is_space);
return {view.begin(), view.end()};
}
[[nodiscard]]
constexpr std::string trim(std::string_view const in)
{
auto view = in
| std::views::drop_while(is_space) | std::views::reverse
| std::views::drop_while(is_space) | std::views::reverse
;
return {view.begin(), view.end()};
}
int main()
{
static_assert(trim_left(" \n C++23") == "C++23"sv);
constexpr std::string_view src = " \f\n\t\r\vHello, C++20!\f\n\t\r\v ";
const auto s = trim(src);
std::cout << "s = " << std::quoted(s) << '\n';
static constexpr auto v = {0, 1, 2, 3, 4, 5};
for (int n : v | std::views::drop_while([](int i) { return i < 3; }))
std::cout << n << ' ';
std::cout << '\n';
}
π Output π
s = "Hello, C++20!"
3 4 5
π₯ Defect reports π₯
these behavior-changing defect reports got applied retroactively to previously published C++ standards, and here's the tea:
- DR
LWG 3494β Applied to:C++20. Behavior as published:drop_while_viewwas never aborrowed_range. Correct behavior: it is aborrowed_rangeif its underlying view is. β¨
π¦ See also π¦
ranges::drop_view,views::dropβ a view consisting of elements of another view, skipping the firstNelements. (C++20) (class template) (range adaptor object) πΈ
that's the whole page, bestie! source/version and date from the footer: http://cppreference.com, 2024.06.10, and the page id std::ranges...p_while_view(3) β 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