Namespaces
Variants

std::ranges:: ends_with

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
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 = { } ) ;
(1) (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 = { } ) ;
(2) (C++23 起)

检查第二个范围是否匹配第一个范围的后缀。

1) N1 ranges:: distance ( first1, last1 ) N2 ranges:: distance ( first2, last2 )
  • N1 < N2 true ,则返回 false
  • 否则返回 ranges:: equal ( std :: move ( first1 ) + ( N1 - N2 ) , last1,
    std :: move ( first2 ) , last2, pred, proj1, proj2 )
2) N1 ranges:: distance ( r1 ) N2 ranges:: distance ( r2 )

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first1, last1 - 定义待检查元素范围的 区间 迭代器-哨位对
r1 - 待检查元素的范围
first2, last2 - 定义用作后缀的元素范围的 区间 迭代器-哨位对
r2 - 用作后缀的元素范围
pred - 比较投影元素的二元谓词
proj1 - 应用于待检查范围元素的投影
proj2 - 应用于用作后缀的范围元素的投影

返回值

若第二个范围匹配第一个范围的后缀,则为 true ;否则为 false

复杂度

通常为线性:最多 min(N1,N2) 次谓词应用及双投影操作。当 N1 < N2 true 时,不会应用谓词和双投影操作。

如果 N1 N2 均可在常数时间内计算(即迭代器-哨位类型对均满足 sized_sentinel_for ,或区间类型均满足 sized_range ),且 N1 < N2 true ,则时间复杂度为常数。

可能的实现

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), last1,
                             std::move(first2), last2,
                             pred, proj1, 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
    {
        const auto n1 = ranges::distance(r1);
        const auto n2 = ranges::distance(r2);
        if (n1 < n2)
            return false;
        return ranges::equal(views::drop(ranges::ref_view(r1),
                                         n1 - static_cast<decltype(n1)>(n2)),
                             r2, pred, proj1, proj2);
    }
};
inline constexpr ends_with_fn ends_with{};

注释

功能测试 标准 功能
__cpp_lib_ranges_starts_ends_with 202106L (C++23) std::ranges::starts_with , std::ranges::ends_with

示例

#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() {}

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 4105 C++23 重载 ( 2 ) 通过 N1 - N2 [1]
计算大小差异
改为
N1 - static_cast < decltype ( N1 ) > ( N2 )
  1. 其结果可能属于 整数类类型 ,这种情况下无法构造 ranges::drop_view

参见

检查一个范围是否以另一个范围开头
(算法函数对象)
(C++20)
检查字符串是否以给定后缀结尾
( std::basic_string<CharT,Traits,Allocator> 的公开成员函数)
(C++20)
检查字符串视图是否以给定后缀结尾
( std::basic_string_view<CharT,Traits> 的公开成员函数)