for_each_n.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. Copyright (c) Marshall Clow 2017.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file for_each_n.hpp
  7. /// \brief Apply a functor to the elements of a sequence
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_FOR_EACH_N_HPP
  10. #define BOOST_ALGORITHM_FOR_EACH_N_HPP
  11. #include <utility> // for std::pair
  12. #include <boost/config.hpp>
  13. namespace boost { namespace algorithm {
  14. /// \fn for_each_n(InputIterator first, Size n, Function f);
  15. /// \return first + n
  16. ///
  17. /// \param first The start of the first range.
  18. /// \param n One past the end of the first range.
  19. /// \param f A functor to apply to the elements of the sequence
  20. /// \note If f returns a result, the result is ignored.
  21. template<class InputIterator, class Size, class Function>
  22. InputIterator for_each_n(InputIterator first, Size n, Function f)
  23. {
  24. for ( ; n > 0; --n, ++first )
  25. f(*first);
  26. return first;
  27. }
  28. }} // namespace boost and algorithm
  29. #endif // BOOST_ALGORITHM_FOR_EACH_N_HPP