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 10 // <algorithm> 11 12 // template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred> 13 // requires OutputIterator<Iter, RvalueOf<Iter::reference>::type> 14 // && CopyConstructible<Pred> 15 // Iter 16 // remove_if(Iter first, Iter last, Pred pred); 17 18 #include <algorithm> 19 #include <functional> 20 #include <cassert> 21 #include <memory> 22 23 #include "test_macros.h" 24 #include "test_iterators.h" 25 #include "counting_predicates.hpp" 26 27 bool equal2 ( int i ) { return i == 2; } 28 29 template <class Iter> 30 void 31 test() 32 { 33 int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2}; 34 const unsigned sa = sizeof(ia)/sizeof(ia[0]); 35 // int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2)); 36 unary_counting_predicate<bool(*)(int), int> cp(equal2); 37 int* r = std::remove_if(ia, ia+sa, std::ref(cp)); 38 assert(r == ia + sa-3); 39 assert(ia[0] == 0); 40 assert(ia[1] == 1); 41 assert(ia[2] == 3); 42 assert(ia[3] == 4); 43 assert(ia[4] == 3); 44 assert(ia[5] == 4); 45 assert(cp.count() == sa); 46 } 47 48 #if TEST_STD_VER >= 11 49 struct pred 50 { 51 bool operator()(const std::unique_ptr<int>& i) {return *i == 2;} 52 }; 53 54 template <class Iter> 55 void 56 test1() 57 { 58 const unsigned sa = 9; 59 std::unique_ptr<int> ia[sa]; 60 ia[0].reset(new int(0)); 61 ia[1].reset(new int(1)); 62 ia[2].reset(new int(2)); 63 ia[3].reset(new int(3)); 64 ia[4].reset(new int(4)); 65 ia[5].reset(new int(2)); 66 ia[6].reset(new int(3)); 67 ia[7].reset(new int(4)); 68 ia[8].reset(new int(2)); 69 Iter r = std::remove_if(Iter(ia), Iter(ia+sa), pred()); 70 assert(base(r) == ia + sa-3); 71 assert(*ia[0] == 0); 72 assert(*ia[1] == 1); 73 assert(*ia[2] == 3); 74 assert(*ia[3] == 4); 75 assert(*ia[4] == 3); 76 assert(*ia[5] == 4); 77 } 78 #endif // TEST_STD_VER >= 11 79 80 int main() 81 { 82 test<forward_iterator<int*> >(); 83 test<bidirectional_iterator<int*> >(); 84 test<random_access_iterator<int*> >(); 85 test<int*>(); 86 87 #if TEST_STD_VER >= 11 88 test1<forward_iterator<std::unique_ptr<int>*> >(); 89 test1<bidirectional_iterator<std::unique_ptr<int>*> >(); 90 test1<random_access_iterator<std::unique_ptr<int>*> >(); 91 test1<std::unique_ptr<int>*>(); 92 #endif // TEST_STD_VER >= 11 93 } 94