Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::ranges::for_each

      From cppreference.com
      <cpp‎ |experimental‎ |ranges
       
       
       
       
      Algorithms library
      Non-modifying sequence operations
                                    
      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 Proj=ranges::identity,

                IndirectUnaryInvocable<projected<I, Proj>> Fun>
      ranges::tagged_pair<tag::in(I),tag::fun(Fun)>

          for_each( I first, S last, Fun f, Proj proj= Proj{});
      (1)(ranges TS)
      template< InputRange R,class Proj=ranges::identity,

                IndirectUnaryInvocable<projected<ranges::iterator_t<R>, Proj>> Fun>
      ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>),tag::fun(Fun)>

          for_each( R&& r, Fun f, Proj proj= Proj{});
      (2)(ranges TS)
      1) Invokes the given function objectf to the result of invoking the projectionproj on dereferencing every iterator in the range[firstlast) (i.e.,ranges::invoke(f,ranges::invoke(proj,*i))), in order.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      For both overloads, if the iterator type is mutable,f may modify the elements of the range through the dereferenced iterator. Iff returns a result, the result is ignored.

      Unlike the rest of the algorithms, for_each is not allowed to make copies of the elements in the sequence even if they are trivially copyable.

      Unlikestd::for_each (which requires onlyMoveConstructible), these functions requireFun to modelCopyConstructible.

      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 to apply the function to
      r - the range to apply the function to
      f - callable object to be applied to each projected element in the range
      proj - projection to apply to the elements

      [edit]Return value

      Atagged_pair object containing the following two members:

      • The first member, with the tagtag::in, is the past-the-end iterator of the source range (that is, an iterator of typeI that compares equal to the sentinellast).
      • The second member, with the tagtag::fun, is initialized fromstd::move(f) (after all applications of the function object).

      [edit]Complexity

      Exactlylast -first applications off andproj.

      [edit]Possible implementation

      template<InputIterator I, Sentinel<I> S,class Proj=ranges::identity,         IndirectUnaryInvocable<ranges::projected<I, Proj>> Fun>auto for_each(I first, S last, Fun f, Proj proj= Proj{})->ranges::tagged_pair<tag::in(I),tag::fun(Fun)>{for(; first!= last;++first)ranges::invoke(f,ranges::invoke(proj,*first));return{std::move(first), std::move(f)};}

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

      applies a function to a range of elements
      (function template)[edit]
      range-for loop(C++11) executes loop over range[edit]
      applies a unaryfunction object to elements from arange
      (function template)[edit]
      (C++17)
      applies a function object to the first N elements of a sequence
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/ranges/algorithm/for_each&oldid=155249"

      [8]ページ先頭

      ©2009-2026 Movatter.jp