std::ranges:: starts_with
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,
|
(1) | (C++23以降) |
|
template
<
ranges::
input_range
R1,
ranges::
input_range
R2,
class
Pred
=
ranges::
equal_to
,
|
(2) | (C++23以降) |
2番目の範囲が最初の範囲の接頭辞と一致するかどうかをチェックします。
N1
と
N2
がそれぞれ
[
first1
,
last1
)
と
[
first2
,
last2
)
の範囲のサイズを表すとする。
N1
<
N2
の場合、
false
を返す。それ以外の場合、範囲
[
first2
,
last2
)
のすべての要素が
[
first1
,
first1
+
N2
)
の対応する要素と等しい場合にのみ
true
を返す。比較は、二項述語
pred
を、それぞれ
proj1
と
proj2
によって投影された二つの範囲の要素に適用して行われる。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定することはできません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| first1, last1 | - | 検査対象の要素の 範囲 を定義するイテレータ-番兵ペア |
| r1 | - | 検査対象の要素の範囲 |
| first2, last2 | - | プレフィックスとして使用される要素の 範囲 を定義するイテレータ-番兵ペア |
| r2 | - | プレフィックスとして使用される要素の範囲 |
| pred | - | 投影された要素を比較する二項述語 |
| proj1 | - | 検査対象範囲の要素に適用する投影 |
| proj2 | - | プレフィックスとして使用される範囲の要素に適用する投影 |
戻り値
true 最初の範囲の接頭辞に2番目の範囲が一致する場合、 false それ以外の場合。
計算量
線形: 最大で min ( N1, N2 ) 回の述語と両方の射影の適用。
実装例
struct starts_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::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 { return ranges::mismatch(std::move(first1), last1, std::move(first2), last2, std::move(pred), std::move(proj1), std::move(proj2) ).in2 == last2; } template<ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires 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 starts_with_fn starts_with {}; |
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_starts_ends_with
|
202106L
|
(C++23) |
std::ranges::starts_with
,
std::ranges::ends_with
|
例
#include <algorithm> #include <iostream> #include <ranges> #include <string_view> int main() { using namespace std::literals; constexpr auto ascii_upper = [](char8_t c) { return u8'a' <= c && c <= u8'z' ? static_cast<char8_t>(c + u8'A' - u8'a') : c; }; constexpr auto cmp_ignore_case = [=](char8_t x, char8_t y) { return ascii_upper(x) == ascii_upper(y); }; static_assert(std::ranges::starts_with("const_cast", "const"sv)); static_assert(std::ranges::starts_with("constexpr", "const"sv)); static_assert(!std::ranges::starts_with("volatile", "const"sv)); std::cout << std::boolalpha << std::ranges::starts_with(u8"Constantinopolis", u8"constant"sv, {}, ascii_upper, ascii_upper) << ' ' << std::ranges::starts_with(u8"Istanbul", u8"constant"sv, {}, ascii_upper, ascii_upper) << ' ' << std::ranges::starts_with(u8"Metropolis", u8"metro"sv, cmp_ignore_case) << ' ' << std::ranges::starts_with(u8"Acropolis", u8"metro"sv, cmp_ignore_case) << '\n'; constexpr static auto v = { 1, 3, 5, 7, 9 }; constexpr auto odd = [](int x) { return x % 2; }; static_assert(std::ranges::starts_with(v, std::views::iota(1) | std::views::filter(odd) | std::views::take(3))); }
出力:
true false true false
関連項目
|
(C++23)
|
範囲が別の範囲で終わるかどうかをチェックする
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
2つの範囲が最初に異なる位置を見つける
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
文字列が指定されたプレフィックスで始まるかどうかをチェックする
(
std::basic_string<CharT,Traits,Allocator>
の公開メンバ関数)
|
|
(C++20)
|
文字列ビューが指定されたプレフィックスで始まるかどうかをチェックする
(
std::basic_string_view<CharT,Traits>
の公開メンバ関数)
|