fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. bool bp (int left, int right) { return ( left + right == 2); }
  7.  
  8. template<class ForwardIt, class BinaryPredicate>
  9. ForwardIt removeif(ForwardIt first, ForwardIt last, BinaryPredicate p) {
  10.  
  11. ForwardIt result = first;
  12. while ( first != last - 1) {
  13. if ( !p( *first, *( first + 1))) {
  14. *result = *first;
  15. ++result;
  16. }
  17. if( first == last - 1) return result;
  18. ++first;
  19. }
  20. return result;
  21. }
  22.  
  23. /*
  24.  *
  25.  */
  26. int main(int argc, char** argv) {
  27.  
  28. int a[] = { 0, 2, 1, 3, 0, 2, 3, 2, 0, 3, 8};
  29. std::vector<int> v( a, a + 11);
  30. std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, ","));
  31. std::cout << std::endl;
  32. std::vector<int>::iterator it = removeif( v.begin(), v.end(), bp);
  33. std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, ","));
  34. v.erase( it, v.end()); std::cout << std::endl;
  35. std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, ","));
  36. return 0;
  37. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
0,2,1,3,0,2,3,2,0,3,8,
2,1,3,2,3,0,3,2,0,3,8,
2,1,3,2,3,0,3,