std::ranges::starts_with() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr bool starts_with( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
constexpr bool starts_with( R1&& r1, R2&& r2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
参数类型是泛型的,并具有以下约束
I1
,I2
-std::input_iterator
S1
,S2
-std::sentinel_for<I1>
,std::sentinel_for<I2>
Pred
- (无)Proj1
,Proj2
- (无)- (2) -
R1
,R2
-std::ranges::input_range
Pred
模板参数对于所有重载都具有默认类型 ranges::equal_to
。
Proj1
和 Proj2
模板参数对于所有重载都具有默认类型 std::identity
。
此外,每个重载都有以下约束
- (1) -
indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
- (2) -
indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2>
(为方便阅读,此处省略了 std::
命名空间)
// (1)
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 starts_with( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
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 starts_with( R1&& r1, R2&& r2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
检查第二个范围是否与第一个范围的前缀匹配。
-
(1) 比较是通过将二元谓词
pred
应用于分别由proj1
和proj2
投影的两个范围中的元素来完成的。 -
(2) 与 (1) 相同,但使用
r1
作为第一个源范围,r2
作为第二个源范围,如同使用ranges::begin(r1)
作为first1
,ranges::end(r1)
作为last1
,ranges::begin(r2)
作为first2
,以及ranges::end(r2)
作为last2
。
本页描述的函数类实体是niebloids。
参数
first1 last1 | 要检查的元素范围。 |
r1 | 要检查的元素范围。 |
first2 last2 | 用作前缀的元素范围。 |
r2 | 用作前缀的元素范围。 |
pred | 用于比较元素的二元谓词。 |
proj1 | 应用于第一个范围中元素的投影。 |
proj2 | 应用于第二个范围中元素的投影。 |
返回值
如果第二个范围与第一个范围的前缀匹配,则为 true
,否则为 false
。
更具体地说
设 N1
和 N2
分别表示范围 [first1
; last1
) 和 [first2
; last2
) 的大小。
- 如果
N1 < N2
,返回false
。 - 否则,如果范围 [
first2
;last2
) 中的每个元素都等于 [first1
;first1 + N2
) 中的相应元素(在投影后使用pred
进行比较),则返回true
。
复杂度
最多应用 min(N1, N2)
次谓词和两次投影。
异常
(无)
可能的实现
starts_with(1) 和 starts_with(2)
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 {};
示例
#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