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 起) |
检查第二个范围是否匹配第一个范围的前缀。
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 ;否则为 false 。
复杂度
Linear: 最多 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)
|
查找两个范围首次出现差异的位置
(算法函数对象) |
|
(C++20)
|
检查字符串是否以给定前缀开头
(
std::basic_string<CharT,Traits,Allocator>
的公开成员函数)
|
|
(C++20)
|
检查字符串视图是否以给定前缀开头
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|