A RetroSearch Logo

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

Search Query:

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

std::ranges::views::cache_latest, std::ranges::cache_latest_view - cppreference.com

std::ranges::cache_latest_view 1)

A range adaptor that caches the last-accessed element of its underlying

view

so that the element does not have to be recomputed on repeated access.

cache_latest_view is an input_range-only that never models borrowed_range or common_range.

[edit] Nested types [edit] Data members Member Description V base_ (private) the underlying view
(exposition-only member object*) non-propagating-cache<CacheT> cache_ (private) an object that caches the result of the last indirection of the current iterator
(exposition-only member object*) [edit] Member functions constructs a cache_latest_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) returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range.
(public 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] gets the address of derived view's data, provided only 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 only 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 only if it satisfies random_access_range
(public member function of std::ranges::view_interface<D>) [edit] std::ranges::cache_latest_view::cache_latest_view (1) (since C++26)

constexpr explicit cache_latest_view( V base );

(2) (since C++26)

1) Value-initializes base_ via its default member initializer (= V()).

2) Initializes base_ with std::move(base).

Parameters std::ranges::cache_latest_view::base (1) (since C++26)

constexpr V base() &&;

(2) (since C++26)

1) Copy-constructs the result from the underlying view. Equivalent to return base_;.

2) Move-constructs the result from the underlying view. Equivalent to return std::move(base_);.

std::ranges::cache_latest_view::begin

constexpr auto begin();

(since C++26)

Equivalent to return /*iterator*/(*this);.

std::ranges::cache_latest_view::end

constexpr auto end();

(since C++26)

Equivalent to return /*sentinel*/(*this);.

std::ranges::cache_latest_view::size [edit] Deduction guides template< class R >
cache_latest_view( R&& ) -> cache_latest_view<views::all_t<R>>;
(since C++26) [edit] Nested classes the iterator type
(exposition-only member class template*) the sentinel type
(exposition-only member class template*) [edit] Notes

cache_latest_view is useful if the computation of the element to produce is expensive.

[edit] Example
#include <algorithm>
#include <print>
#include <ranges>
 
int main()
{
    const auto v = {1, 2, 3, 4, 5};
 
    auto square = [](int i)
    {
        std::print("transform: {} ", i);
        return i * i;
    };
 
    auto is_even = [](int i)
    {
        std::print("filter: {} ", i);
        return i % 2 == 0;
    };
 
    auto print = [](auto v)
    {
        std::ranges::for_each(v, [](int i){ std::println("Got: {}", i); });
        std::println();
    };
 
    std::println("Without caching latest (note recalculations):");
    auto even_squares = v
        | std::views::transform(square)
        | std::views::filter(is_even);
    print(even_squares);
 
    std::println("With caching latest:");
    auto fast_even_squares = v
        | std::views::transform(square)
        | std::views::cache_latest
        | std::views::filter(is_even);
    print(fast_even_squares);
}

Output:

Without caching latest (note recalculations):
transform: 1 filter: 1 transform: 2 filter: 4 transform: 2 Got: 4
transform: 3 filter: 9 transform: 4 filter: 16 transform: 4 Got: 16
transform: 5 filter: 25 
With caching latest:
transform: 1 filter: 1 transform: 2 filter: 4 Got: 4
transform: 3 filter: 9 transform: 4 filter: 16 Got: 16
transform: 5 filter: 25

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