fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. struct Vector {
  6. float x, y, z;
  7. };
  8.  
  9. struct Particle {
  10. int ID;
  11. Vector position { 0.0f, 0.0f, 0.0f };
  12. Vector velocity { 0.0f, 0.0f, 0.0f };
  13. Vector acceleration { 0.0f, 0.0f, 0.0f};
  14. Vector color { 1.0f, 0.0f, 0.0f }; // RED
  15. };
  16.  
  17.  
  18.  
  19.  
  20. int main() {
  21. const int MaxParticles = 5;
  22. Particle particles[MaxParticles] = {{0},{3},{2},{4},{5}};
  23.  
  24. // find particle
  25. int findid = 3;
  26. for(size_t i=0;i< MaxParticles;++i)
  27. {
  28. if (particles[i].ID == findid)
  29. {
  30. std::cout << "found partcile with id " << findid << " at index " << i << std::endl;
  31. break;
  32. }
  33. }
  34.  
  35. auto iter = std::find_if(particles,particles+MaxParticles, [&](const Particle& p){
  36. if (p.ID == findid)
  37. return true;
  38. return false;
  39. });
  40.  
  41. std::cout << "Found particle " << iter->ID << std::endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
found partcile with id 3 at index 1
Found particle 3