copy_n.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. Copyright (c) Marshall Clow 2011-2012.
  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 copy_n.hpp
  7. /// \brief Copy n items from one sequence to another
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_COPY_N_HPP
  10. #define BOOST_ALGORITHM_COPY_N_HPP
  11. #include <algorithm> // for std::copy_n, if available
  12. namespace boost { namespace algorithm {
  13. #if __cplusplus >= 201103L
  14. // Use the C++11 versions of copy_n if it is available
  15. using std::copy_n; // Section 25.3.1
  16. #else
  17. /// \fn copy_n ( InputIterator first, Size n, OutputIterator result )
  18. /// \brief Copies exactly n (n > 0) elements from the range starting at first to
  19. /// the range starting at result.
  20. /// \return The updated output iterator
  21. ///
  22. /// \param first The start of the input sequence
  23. /// \param n The number of elements to copy
  24. /// \param result An output iterator to write the results into
  25. /// \note This function is part of the C++2011 standard library.
  26. /// We will use the standard one if it is available,
  27. /// otherwise we have our own implementation.
  28. template <typename InputIterator, typename Size, typename OutputIterator>
  29. OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
  30. {
  31. for ( ; n > 0; --n, ++first, ++result )
  32. *result = *first;
  33. return result;
  34. }
  35. #endif
  36. }} // namespace boost and algorithm
  37. #endif // BOOST_ALGORITHM_COPY_IF_HPP