Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::search_n

      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
      (1)
      template<std::forward_iterator I,std::sentinel_for<I> S,class T,

               class Pred=ranges::equal_to,class Proj=std::identity>
      requiresstd::indirectly_comparable<I,const T*, Pred, Proj>
      constexprranges::subrange<I>
          search_n( I first, S last,std::iter_difference_t<I> count,

                   const T& value, Pred pred={}, Proj proj={});
      (since C++20)
      (until C++26)
      template<std::forward_iterator I,std::sentinel_for<I> S,

               class Pred=ranges::equal_to,class Proj=std::identity,
               class T= std::projected_value_t<I, Proj>>
      requiresstd::indirectly_comparable<I,const T*, Pred, Proj>
      constexprranges::subrange<I>
          search_n( I first, S last,std::iter_difference_t<I> count,

                   const T& value, Pred pred={}, Proj proj={});
      (since C++26)
      (2)
      template<ranges::forward_range R,class T,

               class Pred=ranges::equal_to,class Proj=std::identity>
      requiresstd::indirectly_comparable
         <ranges::iterator_t<R>,const T*, Pred, Proj>
      constexprranges::borrowed_subrange_t<R>
          search_n( R&& r,ranges::range_difference_t<R> count,

                   const T& value, Pred pred={}, Proj proj={});
      (since C++20)
      (until C++26)
      template<ranges::forward_range R,

               class Pred=ranges::equal_to,class Proj=std::identity,
               class T= std::projected_value_t<ranges::iterator_t<R>, Proj>>
      requiresstd::indirectly_comparable
         <ranges::iterator_t<R>,const T*, Pred, Proj>
      constexprranges::borrowed_subrange_t<R>
          search_n( R&& r,ranges::range_difference_t<R> count,

                   const T& value, Pred pred={}, Proj proj={});
      (since C++26)
      1) Searches the range[firstlast) for thefirst sequence ofcount elements whose projected values are each equal to the givenvalue according to the binary predicatepred.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

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

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining therange of elements to examine (akahaystack)
      r - the range of elements to examine (akahaystack)
      count - the length of the sequence to search for
      value - the value to search for (akaneedle)
      pred - the binary predicate that compares the projected elements withvalue
      proj - the projection to apply to the elements of the range to examine

      [edit]Return value

      1) Returnsstd::ranges::subrange object that contains a pair of iterators in the range[firstlast) that designate the found subsequence.

      If no such subsequence is found, returnsstd::ranges::subrange{last, last}.

      Ifcount<=0, returnsstd::ranges::subrange{first, first}.
      2) Same as(1) but the return type isranges::borrowed_subrange_t<R>.

      [edit]Complexity

      Linear: at mostranges::distance(first, last) applications of the predicate and the projection.

      [edit]Notes

      An implementation can improve efficiency of the searchin average if the iterators modelstd::random_access_iterator.

      Feature-test macroValueStdFeature
      __cpp_lib_algorithm_default_value_type202403(C++26)List-initialization for algorithms

      [edit]Possible implementation

      struct search_n_fn{template<std::forward_iterator I,std::sentinel_for<I> S,class Pred=ranges::equal_to,class Proj=std::identity,class T= std::projected_value_t<I, Proj>>    requiresstd::indirectly_comparable<I,const T*, Pred, Proj>constexprranges::subrange<I>        operator()(I first, S last,std::iter_difference_t<I> count,const T& value, Pred pred={}, Proj proj={})const{if(count<=0)return{first, first};for(; first!= last;++first)if(std::invoke(pred,std::invoke(proj,*first), value)){                I start= first;std::iter_difference_t<I> n{1};for(;;){if(n++== count)return{start,std::next(first)};// foundif(++first== last)return{first, first};// not foundif(!std::invoke(pred,std::invoke(proj,*first), value))break;// not equ to value}}return{first, first};} template<ranges::forward_range R,class Pred=ranges::equal_to,class Proj=std::identity,class T= std::projected_value_t<ranges::iterator_t<R>, Proj>>    requiresstd::indirectly_comparable<ranges::iterator_t<R>,const T*, Pred, Proj>constexprranges::borrowed_subrange_t<R>        operator()(R&& r,ranges::range_difference_t<R> count,const T& value, Pred pred={}, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r),                       std::move(count), value,                       std::move(pred), std::move(proj));}}; inlineconstexpr search_n_fn search_n{};

      [edit]Example

      Run this code
      #include <algorithm>#include <cassert>#include <complex>#include <iomanip>#include <iostream>#include <iterator>#include <string>#include <vector> int main(){namespace ranges= std::ranges; staticconstexprauto nums={1,2,2,3,4,1,2,2,2,1};constexprint count{3};constexprint value{2};typedefint count_t, value_t; constexprauto result1= ranges::search_n(        nums.begin(), nums.end(), count, value);    static_assert// found(        result1.size()== count&&std::distance(nums.begin(), result1.begin())==6&&std::distance(nums.begin(), result1.end())==9); constexprauto result2= ranges::search_n(nums, count, value);    static_assert// found(        result2.size()== count&&std::distance(nums.begin(), result2.begin())==6&&std::distance(nums.begin(), result2.end())==9); constexprauto result3= ranges::search_n(nums, count, value_t{5});    static_assert// not found(        result3.size()==0&&        result3.begin()== result3.end()&&        result3.end()== nums.end()); constexprauto result4= ranges::search_n(nums, count_t{0}, value_t{1});    static_assert// not found(        result4.size()==0&&        result4.begin()== result4.end()&&        result4.end()== nums.begin()); constexprchar symbol{'B'};auto to_ascii=[](constint z)->char{return'A'+ z-1;};auto is_equ=[](constchar x,constchar y){return x== y;}; std::cout<<"Find a sub-sequence "<<std::string(count, symbol)<<" in the ";    std::ranges::transform(nums,std::ostream_iterator<char>(std::cout,""), to_ascii);std::cout<<'\n'; auto result5= ranges::search_n(nums, count, symbol, is_equ, to_ascii);if(not result5.empty())std::cout<<"Found at position "<<ranges::distance(nums.begin(), result5.begin())<<'\n'; std::vector<std::complex<double>> nums2{{4,2},{4,2},{1,3}};#ifdef __cpp_lib_algorithm_default_value_typeauto it= ranges::search_n(nums2,2,{4,2});#elseauto it= ranges::search_n(nums2,2,std::complex<double>{4,2});#endifassert(it.size()==2);}

      Output:

      Find a sub-sequence BBB in the ABBCDABBBAFound at position 6

      [edit]See also

      finds the first two adjacent items that are equal (or satisfy a given predicate)
      (algorithm function object)[edit]
      finds the first element satisfying specific criteria
      (algorithm function object)[edit]
      finds the last sequence of elements in a certain range
      (algorithm function object)[edit]
      searches for any one of a set of elements
      (algorithm function object)[edit]
      returnstrue if one sequence is a subsequence of another
      (algorithm function object)[edit]
      finds the first position where two ranges differ
      (algorithm function object)[edit]
      searches for the first occurrence of a range of elements
      (algorithm function object)[edit]
      searches for the first occurrence of a number consecutive copies of an element in a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/search_n&oldid=180466"

      [8]ページ先頭

      ©2009-2025 Movatter.jp