fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct element_t {
  6. int val;
  7. bool visited;
  8. };
  9.  
  10. bool cmp(const element_t& lhs, const element_t& rhs)
  11. {
  12. if (!lhs.visited && !rhs.visited) return (lhs.val < rhs.val);
  13. return lhs.visited ? rhs.visited : true;
  14.  
  15. /* alternatively:
  16.   return (lhs.visited || rhs.visited)
  17.   ? rhs.visited
  18.   : (lhs.val < rhs.val);
  19. */
  20.  
  21. /* alternatively:
  22. return (!lhs.visited) && (rhs.visited || lhs.val < rhs.val);
  23. */
  24. }
  25.  
  26. int main()
  27. {
  28. std::vector<element_t> vct_priority{
  29. {1,true},
  30. {0,true},
  31. {1,false},
  32. {0,true},
  33. {2,false},
  34. {2147483647,false},
  35. {2147483647,false},
  36. {0,true},
  37. {1,false},
  38. {0,true},
  39. {2,false},
  40. {2147483647,false},
  41. {1,false}
  42. };
  43.  
  44. do
  45. {
  46. auto it = std::min_element(std::begin(vct_priority), std::end(vct_priority), cmp);
  47. if (it == std::end(vct_priority) || it->visited) break;
  48. auto indx_smallest = std::distance(std::begin(vct_priority), it);
  49. std::cout << "index " << indx_smallest << ", value " << it->val << std::endl;
  50. it->visited = true;
  51. }
  52. while (true);
  53.  
  54. std::cout << "done" << std::endl;
  55. return 0;
  56. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
index 2, value 1
index 8, value 1
index 12, value 1
index 4, value 2
index 10, value 2
index 5, value 2147483647
index 6, value 2147483647
index 11, value 2147483647
done