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
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <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以降)

2番目の範囲が最初の範囲の接尾辞と一致するかどうかをチェックします。

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 として知られる)です。すなわち:

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Complexity、Possible implementation、Notes、Example、Defect reports、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先は一切変更していません - 数字や構造は完全に保持されています

パラメータ

first1, last1 - 検査対象の要素の 範囲 を定義するイテレータ-番兵ペア
r1 - 検査対象の要素の範囲
first2, last2 - 接尾辞として使用される要素の 範囲 を定義するイテレータ-番兵ペア
r2 - 接尾辞として使用される要素の範囲
pred - 投影された要素を比較する二項述語
proj1 - 検査対象範囲の要素に適用する投影操作
proj2 - 接尾辞として使用される範囲の要素に適用する投影操作

戻り値

true 第2の範囲が第1の範囲の接尾辞と一致する場合、 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{};
**注記**: 提供されたC++コードはHTMLの`
`タグ内に含まれており、翻訳対象外のテキストとして指定されています。また、HTMLタグや属性、C++固有の用語も翻訳しないように指示されています。したがって、元のコードは完全に保持され、翻訳は行われていません。

注記

機能テスト マクロ 標準 機能
__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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 4105 C++23 オーバーロード ( 2 ) はサイズ差を
計算していた N1 - N2 [1]
以下に変更
N1 - static_cast < decltype ( N1 ) > ( N2 )
  1. その結果は integer-class type である可能性があり、この場合 ranges::drop_view は構築できません。

関連項目

範囲が別の範囲で始まるかどうかをチェックする
(アルゴリズム関数オブジェクト)
(C++20)
文字列が指定された接尾辞で終わるかどうかをチェックする
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)
(C++20)
文字列ビューが指定された接尾辞で終わるかどうかをチェックする
( std::basic_string_view<CharT,Traits> の公開メンバ関数)