Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::ranges::count,std::experimental::ranges::count_if

      From cppreference.com
      <cpp‎ |experimental‎ |ranges
       
       
       
       
      Algorithms library
      Non-modifying sequence operations
      countcount_if
                                    
      Modifying sequence operations
                                    
      Partitioning operations
      Sorting operations
      Binary search operations
      Set operations (on sorted ranges)
                                    
      Heap operations
      Minimum/maximum operations
      Permutations
       
      template< InputIterator I, Sentinel<I> S,class T,class Proj=ranges::identity>

          requires IndirectRelation<ranges::equal_to<>,projected<I, Proj>,const T*>

      ranges::difference_type_t<I> count( I first, S last,const T& value, Proj proj= Proj{});
      (1)(ranges TS)
      template< InputRange R,class T,class Proj=ranges::identity>

          requires IndirectRelation<ranges::equal_to<>,
                                   projected<ranges::iterator_t<R>, Proj>,const T*>
      ranges::difference_type_t<ranges::iterator_t<R>>

          count( R&& r,const T& value, Proj proj= Proj{});
      (2)(ranges TS)
      template< InputIterator I, Sentinel<I> S,class Proj=ranges::identity,

                IndirectUnaryPredicate<projected<I, Proj>> Pred>

      ranges::difference_type_t<I> count_if( I first, S last, Pred pred, Proj proj= Proj{});
      (3)(ranges TS)
      template< InputRange R,class Proj=ranges::identity,

                IndirectUnaryPredicate<projected<ranges::iterator_t<R>, Proj>> Pred>
      ranges::difference_type_t<ranges::iterator_t<R>>

          count_if( R&& r, Pred pred, Proj proj= Proj{});
      (4)(ranges TS)

      Returns the number of elements in the range[firstlast) satisfying specific criteria.

      1) Counts the elements whose projected values are equal tovalue (i.e.,ranges::invoke(proj,*i)== value).
      3) Counts the elements whose projected values satisfy the predicatepred (i.e.,ranges::invoke(pred,ranges::invoke(proj,*i))!=false).
      2,4) Same as(1,3), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable.

      Contents

      [edit]Parameters

      first, last - the range of elements to examine
      r - the range of elements to examine
      value - the value to search for
      pred - predicate to apply to the projected elements
      proj - projection to apply to the elements

      [edit]Return value

      Number of elements satisfying the condition.

      [edit]Complexity

      Exactlylast -first comparisons / applications of the predicate, and the same number of applications of the projection.

      [edit]Notes

      For the number of elements in the range[firstlast) without any additional criteria, seeranges::distance.

      [edit]Possible implementation

      First version
      template<InputIterator I, Sentinel<I> S,class T,class Proj=ranges::identity>    requires IndirectRelation<ranges::equal_to<>,projected<I, Proj>,const T*>ranges::difference_type_t<I> count(I first, S last,const T& value, Proj proj= Proj{}){    ranges::difference_type_t<I> ret=0;for(; first!= last;++first)if(ranges::invoke(proj,*first)== value)++ret;return ret;}
      Second version
      template<InputIterator I, Sentinel<I> S,class Proj=ranges::identity,         IndirectUnaryPredicate<projected<I, Proj>> Pred>ranges::difference_type_t<I> count_if(I first, S last, Pred pred, Proj proj= Proj{}){    ranges::difference_type_t<I> ret=0;for(; first!= last;++first)if(ranges::invoke(pred,ranges::invoke(proj,*i)))++ret;return ret;}

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

      returns the number of elements satisfying specific criteria
      (function template)[edit]
      returns the distance between an iterator and a sentinel, or between the beginning and the end of a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/ranges/algorithm/count&oldid=155240"

      [8]ページ先頭

      ©2009-2025 Movatter.jp