fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Friend {
  7. string first_name;
  8. string last_name;
  9. string phone;
  10. };
  11.  
  12. bool RemoveByName (vector<Friend>& black_book, const string& name) {
  13. vector<Friend>::iterator removed_it = remove_if(black_book.begin(), black_book.end(), [&name](const Friend& f){return f.first_name == name;});
  14. if (removed_it == black_book.end())
  15. return false;
  16. black_book.erase(removed_it, black_book.end());
  17. return true;
  18. }
  19.  
  20. int main() {
  21. vector <Friend> black_book {
  22. Friend {"John", "Nash", "4155555555"},
  23. Friend {"Homer", "Simpson", "2064375555"}
  24. };
  25. if(RemoveByName(black_book, "John")) {
  26. cout << "removed" << endl;
  27. } else {
  28. cout << "not found" << endl;
  29. }
  30. if(RemoveByName(black_book, "Tom")) {
  31. cout << "removed" << endl;
  32. } else {
  33. cout << "not found" << endl;
  34. }
  35. for(int i = 0; i < black_book.size(); ++i) {
  36. Friend& f = black_book.at(i);
  37. cout << f.first_name << " " << f.last_name << " " << f.phone << endl;
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
removed
not found
Homer Simpson 2064375555