std::ranges::ends_with(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

Okay bestie, welcome to the std::ranges::ends_with(3) page from the C++ Standard Libary manual β€” yes, that manual name has the typo, and we are keeping it exactly. The name is literally std::ranges::ends_with, and the NAME line repeats it like std::ranges::ends_with - std::ranges::ends_with. Iconic repetitive behavior, honestly.


✨ SYNOPSIS ✨

Defined in header <algorithm>.

Call signature.

(1) (since C++23)

template< std::input_iterator I1, std::sentinel_for<I1> S1,
          std::input_iterator I2, std::sentinel_for<I2> S2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity, class Proj2 = std::identity >
requires (std::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>)
      && (std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>)
      && std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool
    ends_with( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
               Proj1 proj1 = {}, Proj2 proj2 = {} );

(2) (since C++23)

template< ranges::input_range R1, ranges::input_range R2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity, class Proj2 = std::identity >
requires (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
         (ranges::forward_range<R2> || ranges::sized_range<R2>) &&
         std::indirectly_comparable<ranges::iterator_t<R1>,
                                    ranges::iterator_t<R2>,
                                    Pred, Proj1, Proj2>
constexpr bool
    ends_with( R1&& r1, R2&& r2, Pred pred = {},
               Proj1 proj1 = {}, Proj2 proj2 = {} );

πŸ¦‹ the actual tea πŸ¦‹

This function checks whether the second range matches the suffix of the first range. Suffix matching. That's the whole vibe.

For (1), let N1 and N2 be ranges::distance(first1, last1) and ranges::distance(first2, last2) respectively. If N1 < N2, it returns false. Otherwise, it returns true if and only if every element in the range [first2, last2) is equal to the corresponding element in [first1 + N1 - N2, last1). The comparison is done by applying the binary predicate pred to elements in the two ranges projected by proj1 and proj2 respectively.

For (2), it's the same as (1), but it uses r1 and r2 as the source ranges, as if using ranges::begin(r1) as first1, ranges:begin(r2) as first2 β€” yes, one colon, exactly like the original, bestie β€” ranges::end(r1) as last1, and ranges::end(r2) as last2.

The function-like entities described on this page are niebloids, that is:

  • Explicit template argument lists cannot be specified when calling any of them. Periodt.
  • None of them are visible to argument-dependent lookup.
  • When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.

In practice, they may be implemented as function objects, or with special compiler extensions.


πŸŽ€ PARAMETERS πŸŽ€

  • first1, last1 β€” the range of elements to examine.
  • r1 β€” the range of elements to examine.
  • first2, last2 β€” the range of elements to be used as the suffix.
  • r2 β€” the range of elements to be used as the suffix.
  • pred β€” the binary predicate that compares the projected elements.
  • proj1 β€” the projection to apply to the elements of the range to examine.
  • proj2 β€” the projection to apply to the elements of the range to be used as the suffix.

πŸ’– RETURN VALUE πŸ’–

true if the second range matches the suffix of the first range, false otherwise. That's it. That's the answer. πŸ’…


πŸ”₯ COMPLEXITY πŸ”₯

Generally linear: at most min(N1, N2) applications of the predicate and both projections. The predicate and both projections are not applied if N1 < N2.

If both N1 and N2 can be calculated in constant time (i.e. both iterator-sentinel type pairs model sized_sentinel_for, or both range types model sized_range) and N1 < N2, the time complexity is constant. Slay for constant-time early outs.


🧚 POSSIBLE IMPLEMENTATION 🧚

struct ends_with_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::input_iterator I2, std::sentinel_for<I2> S2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires (std::forward_iterator<I1> ||
              std::sized_sentinel_for<S1, I1>) &&
             (std::forward_iterator<I2> ||
              std::sized_sentinel_for<S2, I2>) &&
             std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
    constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                              Pred pred = {},
                              Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        const auto n1 = ranges::distance(first1, last1);
        const auto n2 = ranges::distance(first2, last2);
        if (n1 < n2)
            return false;
        ranges::advance(first1, n1 - n2);
        return ranges::equal(std::move(first1), std::move(last1),
                             std::move(first2), std::move(last2),
                             std::move(pred), std::move(proj1),
                             std::move(proj2));
    }

    template<ranges::input_range R1, ranges::input_range R2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
             (ranges::forward_range<R2> || ranges::sized_range<R2>) &&
             std::indirectly_comparable<ranges::iterator_t<R1>,
                                        ranges::iterator_t<R2>,
                                        Pred, Proj1, Proj2>
    constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {},
                              Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2),
                       std::move(pred), std::move(proj1),
                       std::move(proj2));
    }
};

inline constexpr ends_with_fn ends_with {};

🌈 NOTES 🌈

Feature-test macro tea:

  • Feature-test macro: __cpp_lib_ranges_starts_ends_with
  • Value: 202106L
  • Std: (C++23)
  • Feature: std::ranges::starts_with, std::ranges::ends_with

πŸ“ EXAMPLE πŸ“

// Run this code

#include <algorithm>
#include <array>

static_assert(
    ! std::ranges::ends_with("for", "cast") &&
    std::ranges::ends_with("dynamic_cast", "cast") &&
    ! std::ranges::ends_with("as_const", "cast") &&
    std::ranges::ends_with("bit_cast", "cast") &&
    ! std::ranges::ends_with("to_underlying", "cast") &&
    std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{3, 4}) &&
    ! std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{4, 5})
);

int main() {}

πŸ‘‘ SEE ALSO πŸ‘‘

  • ranges::starts_with β€” checks whether a range starts with another range (C++23) (niebloid)
  • ends_with β€” checks if the string ends with the given suffix (C++20) (public member function of std::basic_string<CharT,Traits,Allocator>)
  • ends_with β€” checks if the string view ends with the given suffix (C++20) (public member function of std::basic_string_view<CharT,Traits>)

mwah πŸ’‹ this page is from http://cppreference.com, dated 2024.06.10, and it is literally std::ranges::ends_with(3). bye bestie, go match some suffixes 🫢✨

πŸ“¦ where this page comes from