Namespaces
Variants

std::ranges:: ends_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
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur bleibt gemäß den Anforderungen unverändert.)
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 >
erfordert ( std:: forward_iterator < I1 > || std:: sized_sentinel_for < S1, I1 > ) &&
( std:: forward_iterator < I2 > || std:: sized_sentinel_for < S2, I2 > ) &&
std:: indirectly_comparable < I1, I2, Pred, Proj1, Proj2 >
constexpr bool ends_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 >
erfordert ( ranges:: forward_range < R1 > || ranges:: sized_range < R1 > ) &&
( ranges:: forward_range < R2 > || ranges:: sized_range < R2 > ) &&
std:: indirectly_comparable < ranges:: iterator_t < R1 > ,
ranges:: iterator_t < R2 > ,
Pred, Proj1, Proj2 >
constexpr bool ends_with ( R1 && r1, R2 && r2, Pred pred = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (seit C++23)

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

1) Sei N1 gleich ranges:: distance ( first1, last1 ) und N2 gleich ranges:: distance ( first2, last2 ) :
  • Wenn N1 < N2 gleich true ist, wird false zurückgegeben.
  • Andernfalls wird ranges:: equal ( std :: move ( first1 ) + ( N1 - N2 ) , last1,
    std :: move ( first2 ) , last2, pred, proj1, proj2 )
    zurückgegeben.
2) Sei N1 gleich ranges:: distance ( r1 ) und N2 gleich ranges:: distance ( r2 ) .

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 Suffix zu verwendenden Elemente definiert
r2 - der Bereich der als Suffix 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 angewendet wird
proj2 - die Projektion, die auf die Elemente des als Suffix zu verwendenden Bereichs angewendet wird

Rückgabewert

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

Komplexität

Allgemein linear: höchstens min(N1,N2) Anwendungen des Prädikats und beider Projektionen. Das Prädikat und beide Projektionen werden nicht angewendet, wenn N1 < N2 true ist.

Wenn sowohl N1 als auch N2 in konstanter Zeit berechnet werden können (d.h. beide Iterator-Sentinel-Typ-Paare modellieren sized_sentinel_for oder beide Bereichstypen modellieren sized_range ) und N1 < N2 true ist, dann ist die Zeitkomplexität konstant.

Mögliche Implementierung

struct ends_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::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>) &&
             (std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>) &&
             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
    {
        const auto n1 = ranges::distance(first1, last1);
        const auto n2 = ranges::distance(first2, last2);
        if (n1 < n2)
            return false;
        ranges::advance(first1, n1 - n2);
        return ranges::equal(std::move(first1), last1,
                             std::move(first2), last2,
                             pred, proj1, proj2);
    }
    template<ranges::input_range R1, ranges::input_range R2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
             (ranges::forward_range<R2> || ranges::sized_range<R2>) &&
             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
    {
        const auto n1 = ranges::distance(r1);
        const auto n2 = ranges::distance(r2);
        if (n1 < n2)
            return false;
        return ranges::equal(views::drop(ranges::ref_view(r1),
                                         n1 - static_cast<decltype(n1)>(n2)),
                             r2, pred, proj1, proj2);
    }
};
inline constexpr ends_with_fn ends_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 <array>
static_assert
(
    ! std::ranges::ends_with("for", "cast") &&
    std::ranges::ends_with("dynamic_cast", "cast") &&
    ! std::ranges::ends_with("as_const", "cast") &&
    std::ranges::ends_with("bit_cast", "cast") &&
    ! std::ranges::ends_with("to_underlying", "cast") &&
    std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{3, 4}) &&
    ! std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{4, 5})
);
int main() {}

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
LWG 4105 C++23 Überladung ( 2 ) berechnete die Größen-
differenz durch N1 - N2 [1]
geändert zu
N1 - static_cast < decltype ( N1 ) > ( N2 )
  1. Sein Ergebnis könnte von einem integer-class type sein, in diesem Fall kann ranges::drop_view nicht konstruiert werden.

Siehe auch

prüft, ob ein Bereich mit einem anderen Bereich beginnt
(Algorithmus-Funktionsobjekt)
(C++20)
prüft, ob der String mit dem gegebenen Suffix endet
(öffentliche Elementfunktion von std::basic_string<CharT,Traits,Allocator> )
(C++20)
prüft, ob die String-Ansicht mit dem gegebenen Suffix endet
(öffentliche Elementfunktion von std::basic_string_view<CharT,Traits> )