fork(3) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5.  
  6. struct my_struct
  7. {
  8. std::string first_name;
  9. std::string last_name;
  10. };
  11.  
  12. int main()
  13. {
  14. std::vector<my_struct> my_vector(5);
  15.  
  16. my_vector[0].first_name = "David";
  17. my_vector[0].last_name = "Andriw";
  18.  
  19. my_vector[1].first_name = "Jhon";
  20. my_vector[1].last_name = "Monta";
  21.  
  22. my_vector[2].first_name = "Jams";
  23. my_vector[2].last_name = "Ruth";
  24.  
  25. my_vector[3].first_name = "David";
  26. my_vector[3].last_name = "AAA";
  27.  
  28. my_vector[4].first_name = "Jhon";
  29. my_vector[4].last_name = "BBB";
  30.  
  31. for(int i = 0; i < my_vector.size(); i++)
  32. {
  33. int my_count = std::count_if(my_vector.begin(), my_vector.end(),
  34. [&](const my_struct& s) {
  35. return s.first_name == my_vector[i].first_name;
  36. });
  37. std::cout << "First_Name: " << my_vector[i].first_name << "\tCOUNT: " << my_count << std::endl;
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
First_Name: David	COUNT: 2
First_Name: Jhon	COUNT: 2
First_Name: Jams	COUNT: 1
First_Name: David	COUNT: 2
First_Name: Jhon	COUNT: 2