diff options
Diffstat (limited to 'libstdc++-v3/include/bits/stl_algo.h')
-rw-r--r-- | libstdc++-v3/include/bits/stl_algo.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index b50c642f0e6..ca957e0b9a7 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -3867,6 +3867,39 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant. } +#if __cplusplus >= 201703L + /** + * @brief Apply a function to every element of a sequence. + * @ingroup non_mutating_algorithms + * @param __first An input iterator. + * @param __n A value convertible to an integer. + * @param __f A unary function object. + * @return `__first+__n` + * + * Applies the function object `__f` to each element in the range + * `[first, first+n)`. `__f` must not modify the order of the sequence. + * If `__f` has a return value it is ignored. + */ + template<typename _InputIterator, typename _Size, typename _Function> + _InputIterator + for_each_n(_InputIterator __first, _Size __n, _Function __f) + { + auto __n2 = std::__size_to_integer(__n); + using _Cat = typename iterator_traits<_InputIterator>::iterator_category; + if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>) + return std::for_each(__first, __first + __n2, __f); + else + { + while (__n2-->0) + { + __f(*__first); + ++__first; + } + return __first; + } + } +#endif // C++17 + /** * @brief Find the first occurrence of a value in a sequence. * @ingroup non_mutating_algorithms |