Namespaces
Variants

std::ranges:: starts_with

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace 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
Definiert in Header <algorithm>
Aufrufsignatur
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) (seit 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) (seit C++23)

Prüft, ob der zweite Bereich mit dem Präfix des ersten Bereichs übereinstimmt.

1) Seien N1 und N2 die Größen der Bereiche [ first1 , last1 ) und [ first2 , last2 ) . Falls N1 < N2 , wird false zurückgegeben. Andernfalls wird true nur dann zurückgegeben, wenn jedes Element im Bereich [ first2 , last2 ) dem entsprechenden Element in [ first1 , first1 + N2 ) entspricht. Der Vergleich erfolgt durch Anwendung des binären Prädikats pred auf Elemente in beiden Bereichen, die durch proj1 bzw. proj2 projiziert werden.
2) Gleich wie (1) , verwendet jedoch r1 und r2 als Quellbereiche, als ob ranges:: begin ( r1 ) als first1 , ranges:: begin ( r2 ) als first2 , ranges:: end ( r1 ) als last1 und ranges:: end ( r2 ) als last2 verwendet würde.

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

first1, last1 - das Iterator-Sentinel-Paar, das den Bereich der zu untersuchenden Elemente definiert
r1 - der Bereich der zu untersuchenden Elemente
first2, last2 - das Iterator-Sentinel-Paar, das den Bereich der als Präfix zu verwendenden Elemente definiert
r2 - der Bereich der als Präfix zu verwendenden Elemente
pred - das binäre Prädikat, das die projizierten Elemente vergleicht
proj1 - die Projektion, die auf die Elemente des zu untersuchenden Bereichs anzuwenden ist
proj2 - die Projektion, die auf die Elemente des als Präfix zu verwendenden Bereichs anzuwenden ist

Rückgabewert

true wenn der zweite Bereich mit dem Präfix des ersten Bereichs übereinstimmt, false andernfalls.

Komplexität

Linear: höchstens min ( N1, N2 ) Anwendungen des Prädikats und beider Projektionen.

Mögliche Implementierung

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 {};

Hinweise

Feature-Test Makro Wert Std Feature
__cpp_lib_ranges_starts_ends_with 202106L (C++23) std::ranges::starts_with , std::ranges::ends_with

Beispiel

#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)));
}

Ausgabe:

true false true false

Siehe auch

prüft, ob ein Bereich mit einem anderen Bereich endet
(Algorithmus-Funktionsobjekt)
findet die erste Position, an der sich zwei Bereiche unterscheiden
(Algorithmus-Funktionsobjekt)
prüft, ob der String mit dem gegebenen Präfix beginnt
(öffentliche Elementfunktion von std::basic_string<CharT,Traits,Allocator> )
prüft, ob die String-Ansicht mit dem gegebenen Präfix beginnt
(öffentliche Elementfunktion von std::basic_string_view<CharT,Traits> )