fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. class fruit
  6. {
  7. public:
  8. std::string apple;
  9. std::string banana;
  10. std::string orange;
  11. };
  12.  
  13. class grocery
  14. {
  15. public:
  16. std::vector<fruit> g_items;
  17. std::string total_weight;
  18. };
  19.  
  20. int main()
  21. {
  22. std::vector<grocery> shopping;
  23.  
  24. for(int i = 1; i <= 3; ++i)
  25. {
  26. std::string num = std::to_string(i);
  27. grocery g;
  28. g.g_items.push_back({"apple"+num, "banana"+num, "orange"+num});
  29. shopping.push_back(g);
  30. }
  31.  
  32. auto check_item = [&](std::string (fruit::*f_itm)) -> void
  33. {
  34. for (auto &g : shopping)
  35. {
  36. for(auto &f : g.g_items) {
  37. std::cout << f.*f_itm << std::endl;
  38. }
  39. }
  40. std::cout << std::endl;
  41. };
  42.  
  43. check_item(&fruit::apple);
  44. check_item(&fruit::banana);
  45. check_item(&fruit::orange);
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 5652KB
stdin
Standard input is empty
stdout
apple1
apple2
apple3

banana1
banana2
banana3

orange1
orange2
orange3