|
|
|
调用签名
|
|
|
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 = {});
|
(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 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 = {});
|
(2) |
(C++23 起) |
|
|
|
检查第二范围是否匹配第一范围的前缀。
1) 令 N1
与 N2
分别代表范围
[first1, last1)
与 [first2, last2)
的大小。若 N1 < N2 则返回
false 。否则,当且仅当 [first2, last2)
范围中的每个元素均等于 [first1, first1 + N2)
中的对应元素才返回 true 。通过应用二元谓词
pred
到分别由 proj1
与 proj2
投影的两个范围中的元素进行比较。
2) 同, (1) ,但以 r1
与 r2
为源范围,如同以 ranges::begin(r1) 为 first1
,以 ranges:begin(r2) 为 first2
,以 ranges::end(r1) 为 last1
,并以 ranges::end(r2) 为 last2
。
此页面上描述的仿函数实体是 niebloid,即:
实际上,它们能以函数对象,或者某些特殊编译器扩展实现。
参数
first1, last1
|
-
|
要检验的元素范围
|
r1
|
-
|
要检验的元素范围
|
first2, last2
|
-
|
要用作前缀的元素范围
|
r2
|
-
|
要用作前缀的元素范围
|
pred
|
-
|
比较投影后元素的二元谓词
|
proj1
|
-
|
应用到要检验的元素范围的投影
|
proj2
|
-
|
应用到要用作前缀的元素范围的投影
|
返回值
若第二范围匹配第一范围的前缀则为 true ,否则为 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{};
|
示例
#include <string_view>
#include <algorithm>
#include <iostream>
#include <ranges>
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) ) );
}
输出:
参阅
|
检查一个范围是否终于另一范围 (niebloid) |
|
寻找两个范围出现不同的首个位置 (niebloid) |
|
检查字符串是否始于给定前缀 (std::basic_string<CharT,Traits,Allocator>
的公开成员函数) |
|
检查 string_view 是否始于给定前缀 (std::basic_string_view<CharT,Traits>
的公开成员函数) |