fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6.  
  7. void delete_chars_between(std::string& line, char del)
  8. {
  9. std::string::iterator itr_from = std::find(line.begin(), line.end(), del);
  10. // I don't want to pass an iterator to two past the last element
  11. if ( itr_from == line.end() )
  12. return;
  13. std::string::iterator itr_to = std::find(itr_from + 1, line.end(), del);
  14. // ^^^^
  15.  
  16. while ( itr_to != line.end() )
  17. {
  18. itr_to = line.erase(itr_from + 1, itr_to);
  19.  
  20. itr_from = std::find(itr_to + 1, line.end(), del);
  21. // to start another couple ^^^^
  22. if (itr_from == line.end())
  23. break;
  24.  
  25. itr_to = std::find(itr_from + 1, line.end(), del);
  26. }
  27. }
  28.  
  29. int main() {
  30. std::vector<std::pair<std::string, char>> test{
  31. { "hah haaah hah hello!", 'h' },
  32. { "hah haaah hah hello!", 'r' },
  33. { "hah haaah hah hello!", 'e' },
  34. { "hah haaah hah hello!", 'a' }
  35. };
  36.  
  37. for ( auto & t : test ) {
  38. std::cout << "\noriginal: \t" << t.first << "\ndeleting \'" << t.second;
  39. delete_chars_between(t.first, t.second);
  40. std::cout << "\': \t" << t.first << '\n';
  41. }
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
original: 	hah haaah hah hello!
deleting 'h': 	hh hh hh hello!

original: 	hah haaah hah hello!
deleting 'r': 	hah haaah hah hello!

original: 	hah haaah hah hello!
deleting 'e': 	hah haaah hah hello!

original: 	hah haaah hah hello!
deleting 'a': 	haaaah hah hello!