fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. std::size_t getIndexFor(const std::vector<int>& v, int input)
  6. {
  7. auto it = std::find_if(v.begin(), v.end(), [&](int e){ return e > input;});
  8. if (it == v.begin()) {
  9. throw std::runtime_error("input not handled");
  10. }
  11. --it;
  12. return std::distance(v.begin(), it);
  13. }
  14.  
  15. int main()
  16. {
  17. const std::vector<int> v = { 0, 4};
  18.  
  19. for (int i : {0, 1, 2, 3, 4, 5}) {
  20. std::cout << getIndexFor(v, i) << ":" << i << std::endl;
  21. }
  22. }
  23.  
Success #stdin #stdout 0s 4364KB
stdin
Standard input is empty
stdout
0:0
0:1
0:2
0:3
1:4
1:5