replace_if.pass.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. bool equalToTwo(int v) { return v == 2; }
  21. template <class Iter>
  22. void
  23. test()
  24. {
  25. int ia[] = {0, 1, 2, 3, 4};
  26. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  27. std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);
  28. assert(ia[0] == 0);
  29. assert(ia[1] == 1);
  30. assert(ia[2] == 5);
  31. assert(ia[3] == 3);
  32. assert(ia[4] == 4);
  33. }
  34. int main()
  35. {
  36. test<forward_iterator<int*> >();
  37. test<bidirectional_iterator<int*> >();
  38. test<random_access_iterator<int*> >();
  39. test<int*>();
  40. }