partition_copy.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 partition_copy.hpp
  7. /// \brief Copy a subset of a sequence to a new sequence
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP
  10. #define BOOST_ALGORITHM_PARTITION_COPY_HPP
  11. #include <utility> // for std::pair
  12. #include <boost/config.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost { namespace algorithm {
  16. /// \fn partition_copy ( InputIterator first, InputIterator last,
  17. /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  18. /// \brief Copies the elements that satisfy the predicate p from the range [first, last)
  19. /// to the range beginning at d_first_true, and
  20. /// copies the elements that do not satisfy p to the range beginning at d_first_false.
  21. ///
  22. ///
  23. /// \param first The start of the input sequence
  24. /// \param last One past the end of the input sequence
  25. /// \param out_true An output iterator to write the elements that satisfy the predicate into
  26. /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
  27. /// \param p A predicate for dividing the elements of the input sequence.
  28. ///
  29. /// \note This function is part of the C++2011 standard library.
  30. template <typename InputIterator,
  31. typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
  32. BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
  33. partition_copy ( InputIterator first, InputIterator last,
  34. OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  35. {
  36. for ( ; first != last; ++first )
  37. if ( p (*first))
  38. *out_true++ = *first;
  39. else
  40. *out_false++ = *first;
  41. return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false );
  42. }
  43. /// \fn partition_copy ( const Range &r,
  44. /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  45. ///
  46. /// \param r The input range
  47. /// \param out_true An output iterator to write the elements that satisfy the predicate into
  48. /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
  49. /// \param p A predicate for dividing the elements of the input sequence.
  50. ///
  51. template <typename Range, typename OutputIterator1, typename OutputIterator2,
  52. typename UnaryPredicate>
  53. BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
  54. partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false,
  55. UnaryPredicate p )
  56. {
  57. return boost::algorithm::partition_copy
  58. (boost::begin(r), boost::end(r), out_true, out_false, p );
  59. }
  60. }} // namespace boost and algorithm
  61. #endif // BOOST_ALGORITHM_PARTITION_COPY_HPP