find_if_not.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 find_if_not.hpp
  7. /// \brief Find the first element in a sequence that does not satisfy a predicate.
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_FIND_IF_NOT_HPP
  10. #define BOOST_ALGORITHM_FIND_IF_NOT_HPP
  11. #include <algorithm> // for std::find_if_not, if it exists
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. namespace boost { namespace algorithm {
  15. #if __cplusplus >= 201103L
  16. // Use the C++11 versions of find_if_not if it is available
  17. using std::find_if_not; // Section 25.2.5
  18. #else
  19. /// \fn find_if_not(InputIterator first, InputIterator last, Predicate p)
  20. /// \brief Finds the first element in the sequence that does not satisfy the predicate.
  21. /// \return The iterator pointing to the desired element.
  22. ///
  23. /// \param first The start of the input sequence
  24. /// \param last One past the end of the input sequence
  25. /// \param p A predicate for testing the elements of the range
  26. /// \note This function is part of the C++2011 standard library.
  27. /// We will use the standard one if it is available,
  28. /// otherwise we have our own implementation.
  29. template<typename InputIterator, typename Predicate>
  30. InputIterator find_if_not ( InputIterator first, InputIterator last, Predicate p )
  31. {
  32. for ( ; first != last; ++first )
  33. if ( !p(*first))
  34. break;
  35. return first;
  36. }
  37. #endif
  38. /// \fn find_if_not ( const Range &r, Predicate p )
  39. /// \brief Finds the first element in the sequence that does not satisfy the predicate.
  40. /// \return The iterator pointing to the desired element.
  41. ///
  42. /// \param r The input range
  43. /// \param p A predicate for testing the elements of the range
  44. ///
  45. template<typename Range, typename Predicate>
  46. typename boost::range_iterator<const Range>::type find_if_not ( const Range &r, Predicate p )
  47. {
  48. return boost::algorithm::find_if_not (boost::begin (r), boost::end(r), p);
  49. }
  50. }}
  51. #endif // BOOST_ALGORITHM_FIND_IF_NOT_HPP