std::ranges::ends_with
| ヘッダ <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 番目の範囲が最初の範囲の接尾辞と一致するかどうかを調べます。
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)を返す。
N1 を ranges::distance(r1) とし、N2 を ranges::distance(r2) とする。
N1 < N2がtrueの場合、falseを返す。- それ以外の場合、
ranges::equal(views::drop(ranges::ref_view(r1),N1 - static_cast<decltype(N1)>(N2)),r2, pred, proj1, proj2)を返す。
このページに記載されている関数状のエンティティは、アルゴリズム関数オブジェクト (非公式に niebloids として知られる) です。すなわち:
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- それらのいずれも 引数依存の名前探索 (ADL) には可視ではありません。
- それらのいずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つけられた場合、引数依存の名前探索 (ADL) は抑制されます。
パラメータ
| 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{}; |
`タグ内に含まれており、翻訳対象外のテキストとして指定されています。また、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 ) |
- ↑ その結果は integer-class type である可能性があり、この場合 ranges::drop_view は構築できません。
関連項目
|
(C++23)
|
範囲が別の範囲で始まるかどうかをチェックする
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
文字列が指定された接尾辞で終わるかどうかをチェックする
( std::basic_string<CharT,Traits,Allocator>の公開メンバ関数)
|
|
(C++20)
|
文字列ビューが指定された接尾辞で終わるかどうかをチェックする
( std::basic_string_view<CharT,Traits>の公開メンバ関数)
|