fork download
  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5.  
  6. template <typename T>
  7. auto firstInactive(const std::vector<T>& v)
  8. -> typename std::vector<T>::const_iterator // for c++11
  9. {
  10. return std::find_if(v.begin(), v.end(), [](const T& e) { return !e.active; });
  11. }
  12.  
  13. struct ChildTest_1{
  14. bool active = false;
  15. };
  16.  
  17. struct ChildTest_2{
  18. bool active = true;
  19. };
  20.  
  21.  
  22. int main()
  23. {
  24. std::vector<ChildTest_1> allTest1(10);
  25. std::vector<ChildTest_2> allTest2(10);
  26.  
  27. allTest1[0].active = true;
  28. allTest2[4].active = false;
  29.  
  30. auto it1 = firstInactive(allTest1);
  31. auto it2 = firstInactive(allTest2);
  32.  
  33. if (it1 != allTest1.end()) {
  34. std::cout << "First inactive in alltest1 is "
  35. << std::distance(allTest1.cbegin(), it1) << std::endl;
  36. }
  37. if (it2 != allTest2.end()) {
  38. std::cout << "First inactive in alltest2 is "
  39. << std::distance(allTest2.cbegin(), it2) << std::endl;
  40. }
  41. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
First inactive in alltest1 is 1
First inactive in alltest2 is 4