std::ranges::starts_with() algorithm
- od C++20
- Simplified
- Detailed
// (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 = {} );
The type of arguments are generic and have the following constraints:
I1,I2-std::input_iteratorS1,S2-std::sentinel_for<I1>,std::sentinel_for<I2>Pred- (none)Proj1,Proj2- (none)- (2) -
R1,R2-std::ranges::input_range
The Pred template argumenth as a default type of ranges::equal_to for all overloads.
The Proj1 and Proj2 template arguments have a default type of std::identity for all overloads.
Additionally, each overload has the following constraints:
- (1) -
indirectly_comparable<I1, I2, Pred, Proj1, Proj2> - (2) -
indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2>
(The std:: namespace was ommitted here for readability)
// (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 = {} );
Checks whether the second range matches the prefix of the first range.
-
(1) Comparison is done by applying the binary predicate
predto elements in two ranges projected byproj1andproj2respectively. -
(2) Same as (1), but uses
r1as the first source range andr2as the second source range, as if usingranges::begin(r1)asfirst1,ranges::end(r1)aslast1,ranges::begin(r2)asfirst2, andranges::end(r2)aslast2.
The function-like entities described on this page are niebloids.
Parameters
first1 last1 | The range of elements to examine. |
r1 | The range of elements to examine. |
first2 last2 | The range of elements to be used as prefix. |
r2 | The range of elements to be used as prefix. |
pred | Binary predicate to compare the elements with. |
proj1 | Projection to apply to the elements in the first range. |
proj2 | Projection to apply to the elements in the second range. |
Return value
true if the second range matches the prefix of the first range, false otherwise.
More specifically:
Let N1 and N2 denote the size of ranges [first1; last1) and [first2; last2) respectively.
- If
N1 < N2, returnsfalse. - Otherwise, if every element in the range [
first2;last2) is equal to the corresponding element in [first1;first1 + N2) (compared usingpredafter projections), returnstrue.
Complexity
At most min(N1, N2) applications of the predicate and both projections.
Exceptions
(none)
Possible implementation
starts_with(1) and starts_with(2)
Examples
#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
Hover to see the original license.