replace_if.pass.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred, class T>
  11. // requires OutputIterator<Iter, Iter::reference>
  12. // && OutputIterator<Iter, const T&>
  13. // && CopyConstructible<Pred>
  14. // void
  15. // replace_if(Iter first, Iter last, Pred pred, const T& new_value);
  16. #include <algorithm>
  17. #include <functional>
  18. #include <cassert>
  19. #include "test_iterators.h"
  20. template <class Iter>
  21. void
  22. test()
  23. {
  24. int ia[] = {0, 1, 2, 3, 4};
  25. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  26. std::replace_if(Iter(ia), Iter(ia+sa), std::bind2nd(std::equal_to<int>(), 2), 5);
  27. assert(ia[0] == 0);
  28. assert(ia[1] == 1);
  29. assert(ia[2] == 5);
  30. assert(ia[3] == 3);
  31. assert(ia[4] == 4);
  32. }
  33. int main()
  34. {
  35. test<forward_iterator<int*> >();
  36. test<bidirectional_iterator<int*> >();
  37. test<random_access_iterator<int*> >();
  38. test<int*>();
  39. }