Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::ends_with

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(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
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::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
       
      Defined in header<algorithm>
      Call signature
      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>
      constexprbool ends_with( I1 first1, S1 last1,
                                I2 first2, S2 last2, Pred pred={},

                                Proj1 proj1={}, Proj2 proj2={});
      (1)(since 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(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>
      constexprbool ends_with( R1&& r1, R2&& r2, Pred pred={},

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

      Checks whether the second range matches the suffix of the first range.

      1) LetN1 beranges::distance(first1, last1) andN2 beranges::distance(first2, last2):
      • IfN1< N2 istrue, returnsfalse.
      • Otherwise, returnsranges::equal(std::move(first1)+(N1- N2), last1,
                      std::move(first2), last2, pred, proj1, proj2)
        .
      2) LetN1 beranges::distance(r1) andN2 beranges::distance(r2).
      • IfN1< N2 istrue, returnsfalse.
      • Otherwise, returnsranges::equal(views::drop(ranges::ref_view(r1),
                                  N1-static_cast<decltype(N1)>(N2)),
                      r2, pred, proj1, proj2)
        .

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first1, last1 - the iterator-sentinel pair defining therange of elements to examine
      r1 - the range of elements to examine
      first2, last2 - the iterator-sentinel pair defining therange of elements to be used as the suffix
      r2 - the range of elements to be used as the suffix
      pred - the binary predicate that compares the projected elements
      proj1 - the projection to apply to the elements of the range to examine
      proj2 - the projection to apply to the elements of the range to be used as the suffix

      [edit]Return value

      true if the second range matches the suffix of the first range,false otherwise.

      [edit]Complexity

      Generally linear: at most\(\scriptsize \min(N1,N2) \)min(N1,N2) applications of the predicate and both projections. The predicate and both projections are not applied ifN1< N2 istrue.

      If bothN1 andN2 can be calculated in constant time (i.e. both iterator-sentinel type pairs modelsized_sentinel_for, or both range types modelsized_range) andN1< N2 istrue, the time complexity is constant.

      [edit]Possible implementation

      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>constexprbool operator()(I1 first1, S1 last1, I2 first2, S2 last2,                              Pred pred={}, Proj1 proj1={}, Proj2 proj2={})const{constauto n1=ranges::distance(first1, last1);constauto n2=ranges::distance(first2, last2);if(n1< n2)returnfalse;ranges::advance(first1, n1- n2);returnranges::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>constexprbool operator()(R1&& r1, R2&& r2,                              Pred pred={}, Proj1 proj1={}, Proj2 proj2={})const{constauto n1=ranges::distance(r1);constauto n2=ranges::distance(r2);if(n1< n2)returnfalse;returnranges::equal(views::drop(ranges::ref_view(r1),                                         n1-static_cast<decltype(n1)>(n2)),                             r2, pred, proj1, proj2);}}; inlineconstexpr ends_with_fn ends_with{};

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_ranges_starts_ends_with202106L(C++23)std::ranges::starts_with,std::ranges::ends_with

      [edit]Example

      Run this code
      #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(){}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 4105C++23overload(2) calculated the size
      difference byN1- N2[1]
      changed to
      N1-static_cast<decltype(N1)>(N2)
      1. Its result might be of aninteger-class type, in this caseranges::drop_view cannot be constructed.

      [edit]See also

      checks whether a range starts with another range
      (algorithm function object)[edit]
      (C++20)
      checks if the string ends with the given suffix
      (public member function ofstd::basic_string<CharT,Traits,Allocator>)[edit]
      (C++20)
      checks if the string view ends with the given suffix
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/ends_with&oldid=180468"

      [8]ページ先頭

      ©2009-2025 Movatter.jp