Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::starts_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>
      requiresstd::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
      constexprbool
          starts_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>
      requiresstd::indirectly_comparable<ranges::iterator_t<R1>,
                                         ranges::iterator_t<R2>,
                                          Pred, Proj1, Proj2>
      constexprbool
          starts_with( R1&& r1, R2&& r2, Pred pred={},

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

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

      1) LetN1 andN2 denote the size of ranges[first1last1) and[first2last2) respectively. IfN1< N2, returnsfalse. Otherwise, returnstrue only if every element in the range[first2last2) is equal to the corresponding element in[first1first1+ N2). Comparison is done by applying the binary predicatepred to elements in two ranges projected byproj1 andproj2 respectively.
      2) Same as(1), but usesr1 andr2 as the source ranges, as if usingranges::begin(r1) asfirst1,ranges::begin(r2) asfirst2,ranges::end(r1) aslast1, andranges::end(r2) aslast2.

      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 prefix
      r2 - the range of elements to be used as the prefix
      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 prefix

      [edit]Return value

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

      [edit]Complexity

      Linear: at mostmin(N1, N2) applications of the predicate and both projections.

      [edit]Possible implementation

      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>    requiresstd::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>constexprbool operator()(I1 first1, S1 last1, I2 first2, S2 last2,                              Pred pred={}, Proj1 proj1={}, Proj2 proj2={})const{returnranges::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>    requiresstd::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{return(*this)(ranges::begin(r1),ranges::end(r1),ranges::begin(r2),ranges::end(r2),                       std::move(pred), std::move(proj1), std::move(proj2));}}; inlineconstexpr starts_with_fn starts_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 <iostream>#include <ranges>#include <string_view> int main(){usingnamespace std::literals; constexprauto ascii_upper=[](char8_t c){return u8'a'<= c&& c<= u8'z'?static_cast<char8_t>(c+ u8'A'- u8'a'): c;}; constexprauto 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'; constexprstaticauto v={1,3,5,7,9};constexprauto 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)));}

      Output:

      true false true false

      [edit]See also

      checks whether a range ends with another range
      (algorithm function object)[edit]
      finds the first position where two ranges differ
      (algorithm function object)[edit]
      checks if the string starts with the given prefix
      (public member function ofstd::basic_string<CharT,Traits,Allocator>)[edit]
      checks if the string view starts with the given prefix
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/starts_with&oldid=182831"

      [8]ページ先頭

      ©2009-2025 Movatter.jp