fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4.  
  5.  
  6.  
  7. using namespace std;
  8.  
  9. template<typename Cont, typename It, class Pr>
  10. void erase_neighbors(Cont &cont, It first, It last, int nSpan, Pr Pred)
  11. {
  12. if (0 < nSpan && nSpan < std::distance(first, last)) for (It it2; (it2 = first), first != last; )
  13. {
  14. if (nSpan < std::distance(it2, last))
  15. {
  16. std::advance(it2, nSpan);
  17. if (Pred(*first, *it2))
  18. {
  19. first = cont.erase(first, it2);
  20. last = cont.end();
  21. continue;
  22. }
  23. }
  24. ++first;
  25. }
  26. }
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32. std::vector<char> vc;
  33.  
  34. vc.push_back('a');
  35. vc.push_back('b');
  36. vc.push_back('a');
  37. vc.push_back('d');
  38. vc.push_back('e');
  39. vc.push_back('d');
  40. vc.push_back('f');
  41.  
  42. std::copy(vc.begin(), vc.end(), std::ostream_iterator<char>(std::cout, ", "));
  43. std::cout << std::endl;
  44.  
  45. erase_neighbors(vc, vc.begin(), vc.end(), 2, [](char rhs, char lhs) {
  46. return rhs == lhs;
  47. });
  48.  
  49. std::copy(vc.begin(), vc.end(), std::ostream_iterator<char>(std::cout, ", "));
  50. std::cout << std::endl;
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
a, b, a, d, e, d, f, 
a, d, f,