A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/ranges/../../ranges/chunk_by_view.html below:

std::ranges::views::chunk_by, std::ranges::chunk_by_view - cppreference.com

std::ranges::chunk_by_view 1) chunk_by_view

is a range adaptor that takes a

view

and an invocable object

pred

(the binary predicate), and produces a

view

of subranges (chunks), by splitting the underlying view between each pair of adjacent elements for which

pred

returns

false

. The first element of each such pair belongs to the previous chunk, and the second element belongs to the next chunk.

chunk_by_view always models forward_range, and models bidirectional_range and/or common_range, if adapted view type models the corresponding concepts. chunk_by_view never models borrowed_range or sized_range.

[edit] Data members Member Definition V base_ the underlying view
(exposition-only member object*) movable-box<Pred> pred_ an object that wraps the predicate used to split the elements of base_
(exposition-only member object*) non-propagating-cache<iterator> begin_ an object that caches the iterator to the first element
(exposition-only member object*) [edit] Member functions constructs a chunk_by_view
(public member function) [edit] returns a copy of the underlying (adapted) view
(public member function) [edit] returns a reference to the stored predicate
(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 an iterator to the begin of the next subrange
(exposition-only member function*) returns an iterator to the begin of the previous subrange
(exposition-only member function*) Inherited from std::ranges::view_interface returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range
(public member function of std::ranges::view_interface<D>) [edit] returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>) [edit] 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 only if ranges::empty is applicable to it
(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 only if it satisfies bidirectional_range and common_range
(public member function of std::ranges::view_interface<D>) [edit] [edit] Deduction guides [edit] Nested classes the iterator type
(exposition-only member class template*) [edit] Notes

In order to provide the amortized constant time complexity required by the range concept, the result of begin() is cached within the chunk_by_view object. If the underlying range is modified after the first call to begin(), subsequent uses of the chunk_by_view object might have unintuitive behavior.

[edit] Example
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
 
void print_chunks(auto view, std::string_view separator = ", ")
{
    for (auto const subrange : view)
    {
        std::cout << '[';
        for (std::string_view prefix; auto const& elem : subrange)
            std::cout << prefix << elem, prefix = separator;
        std::cout << "] ";
    }
    std::cout << '\n';
}
 
int main()
{
    std::initializer_list v1 = {1, 2, 3, 1, 2, 3, 3, 3, 1, 2, 3};
    auto fn1 = std::ranges::less{};
    auto view1 = v1 | std::views::chunk_by(fn1);
    print_chunks(view1);
 
    std::initializer_list v2 = {1, 2, 3, 4, 4, 0, 2, 3, 3, 3, 2, 1};
    auto fn2 = std::ranges::not_equal_to{};
    auto view2 = v2 | std::views::chunk_by(fn2);
    print_chunks(view2);
 
    std::string_view v3 = "__cpp_lib_ranges_chunk_by";
    auto fn3 = [](auto x, auto y) { return not(x == '_' or y == '_'); };
    auto view3 = v3 | std::views::chunk_by(fn3);
    print_chunks(view3, "");
 
    std::string_view v4 = "\u007a\u00df\u6c34\u{1f34c}"; // "zß水🍌"
    auto fn4 = [](auto, auto ß) { return 128 == ((128 + 64) & ß); };
    auto view4 = v4 | std::views::chunk_by(fn4);
    print_chunks(view4, "");
}

Output:

[1, 2, 3] [1, 2, 3] [3] [3] [1, 2, 3] 
[1, 2, 3, 4] [4, 0, 2, 3] [3] [3, 2, 1] 
[_] [_] [cpp] [_] [lib] [_] [ranges] [_] [chunk] [_] [by]
[z] [ß] [水] [🍌]
[edit] References
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4