fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. class Car
  7. {
  8. private:
  9. int newId;
  10. std::string newName;
  11. float newPrice;
  12. std::string newSessUserName;
  13. public:
  14. Car(const int& a, const std::string& b, const float& c, const std::string& d)
  15. :newId(a), newName(b), newPrice(c), newSessUserName(d) {} // use initilizer list constructor
  16. const int& getnewId()const { return newId; }
  17. const std::string& getnewName()const { return newName; }
  18. const float& getnewPrice()const { return newPrice; }
  19. const std::string& getnewSessUserName()const{ return newSessUserName; }
  20. };
  21.  
  22. int main()
  23. {
  24. std::vector< Car > carVec =
  25. { {1, "name1", 101.0f, "username1"},
  26. {3, "name3", 103.0f, "username3"},
  27. {2, "name2", 102.0f, "username2"},
  28. {2, "name2", 102.0f, "username2"},
  29. };
  30.  
  31. int id_to_delete = 2;
  32.  
  33. carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
  34. {
  35. return ele.getnewId() == id_to_delete;
  36. }), carVec.end());
  37.  
  38. for(const auto& it: carVec)
  39. std::cout << it.getnewId() << " " << it.getnewName() << " " << it.getnewPrice() << " " << it.getnewSessUserName() << "\n";
  40. return 0;
  41. }
  42.  
  43.  
  44.  
Success #stdin #stdout 0s 4376KB
stdin
Standard input is empty
stdout
1 name1 101 username1
3 name3 103 username3