On this page
std::ranges::ends_with
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Checks whether the second range matches the suffix of the first range.
N1
and N2
be ranges::distance(first1, last1)
and ranges::distance(first2, last2)
respectively. If N1 < N2
, returns false
. Otherwise, returns true
if only if every element in the range [
first2
,
last2
)
is equal to the corresponding element in [
first1 + N1 - N2
,
last1
)
. Comparison is done by applying the binary predicate pred
to elements in two ranges projected by proj1
and proj2
respectively.
r1
and r2
as the source ranges, as if using ranges::begin(r1)
as first1
, ranges:begin(r2)
as first2
, 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.
- 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.
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.
Possible implementation
|
Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_starts_ends_with |
202106L | (C++23) | std::ranges::starts_with , std::ranges::ends_with |
Example
#include <algorithm>
#include <array>
#include <iostream>
int main()
{
std::cout
<< std::boolalpha
<< std::ranges::ends_with("static_cast", "cast") << '\n'
<< std::ranges::ends_with("const_cast", "cast") << '\n'
<< std::ranges::ends_with("reinterpret_cast", "cast") << '\n'
<< std::ranges::ends_with("dynamic_cast", "cast") << '\n'
<< std::ranges::ends_with("move", "cast") << '\n'
<< std::ranges::ends_with("move_if_noexcept", "cast") << '\n'
<< std::ranges::ends_with("forward", "cast") << '\n';
static_assert(
! std::ranges::ends_with("as_const", "cast") and
!! std::ranges::ends_with("bit_cast", "cast") and
! std::ranges::ends_with("to_underlying", "cast") and
!! std::ranges::ends_with(std::array {1,2,3,4}, std::array {3,4}) and
! std::ranges::ends_with(std::array {1,2,3,4}, std::array {4,5})
);
}
Output:
true
true
true
true
false
false
false
See also
(C++23)
|
checks whether a range starts with another range (niebloid) |
(C++20)
|
checks if the string ends with the given suffix (public member function of std::basic_string<CharT,Traits,Allocator> ) |
(C++20)
|
checks if the string view ends with the given suffix (public member function of std::basic_string_view<CharT,Traits> ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/ends_with