mismatch.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright (c) Marshall Clow 2008-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 mismatch.hpp
  7. /// \brief Find the first mismatched element in a sequence
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_MISMATCH_HPP
  10. #define BOOST_ALGORITHM_MISMATCH_HPP
  11. #include <utility> // for std::pair
  12. #include <boost/config.hpp>
  13. namespace boost { namespace algorithm {
  14. /// \fn mismatch ( InputIterator1 first1, InputIterator1 last1,
  15. /// InputIterator2 first2, InputIterator2 last2,
  16. /// BinaryPredicate pred )
  17. /// \return a pair of iterators pointing to the first elements in the sequence that do not match
  18. ///
  19. /// \param first1 The start of the first range.
  20. /// \param last1 One past the end of the first range.
  21. /// \param first2 The start of the second range.
  22. /// \param last2 One past the end of the second range.
  23. /// \param pred A predicate for comparing the elements of the ranges
  24. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  25. BOOST_CXX14_CONSTEXPR std::pair<InputIterator1, InputIterator2> mismatch (
  26. InputIterator1 first1, InputIterator1 last1,
  27. InputIterator2 first2, InputIterator2 last2,
  28. BinaryPredicate pred )
  29. {
  30. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  31. if ( !pred ( *first1, *first2 ))
  32. break;
  33. return std::pair<InputIterator1, InputIterator2>(first1, first2);
  34. }
  35. /// \fn mismatch ( InputIterator1 first1, InputIterator1 last1,
  36. /// InputIterator2 first2, InputIterator2 last2 )
  37. /// \return a pair of iterators pointing to the first elements in the sequence that do not match
  38. ///
  39. /// \param first1 The start of the first range.
  40. /// \param last1 One past the end of the first range.
  41. /// \param first2 The start of the second range.
  42. /// \param last2 One past the end of the second range.
  43. template <class InputIterator1, class InputIterator2>
  44. BOOST_CXX14_CONSTEXPR std::pair<InputIterator1, InputIterator2> mismatch (
  45. InputIterator1 first1, InputIterator1 last1,
  46. InputIterator2 first2, InputIterator2 last2 )
  47. {
  48. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  49. if ( *first1 != *first2 )
  50. break;
  51. return std::pair<InputIterator1, InputIterator2>(first1, first2);
  52. }
  53. // There are already range-based versions of these.
  54. }} // namespace boost and algorithm
  55. #endif // BOOST_ALGORITHM_MISMATCH_HPP