fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. struct Appointment
  8. {
  9. Appointment(const std::string& a_name,
  10. const std::string& a_location) : name(a_name),
  11. location(a_location) {}
  12. std::string name;
  13. std::string location;
  14. };
  15.  
  16. int main()
  17. {
  18. std::vector<Appointment> appointments;
  19. appointments.push_back(Appointment("fred", "belfast"));
  20. appointments.push_back(Appointment("ivor", "london"));
  21.  
  22. // Search by name.
  23. //
  24. const std::string name_to_find = "ivor";
  25. auto i = std::find_if(appointments.begin(),
  26. appointments.end(),
  27. [&](const Appointment& a_app)
  28. {
  29. return name_to_find == a_app.name;
  30. });
  31. if (i != appointments.end())
  32. {
  33. std::cout << i->name << ", " << i->location << "\n";
  34. }
  35.  
  36. // Search by location.
  37. //
  38. const std::string location_to_find = "belfast";
  39. i = std::find_if(appointments.begin(),
  40. appointments.end(),
  41. [&](const Appointment& a_app)
  42. {
  43. return location_to_find == a_app.location;
  44. });
  45. if (i != appointments.end())
  46. {
  47. std::cout << i->name << ", " << i->location << "\n";
  48. }
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
ivor, london
fred, belfast