fork(2) 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::lower_bound(v.begin(), v.end(), input);
  8. if (it != v.begin() && (it == v.end() || *it != input)) {
  9. --it;
  10. }
  11. return std::distance(v.begin(), it);
  12. }
  13.  
  14. int main()
  15. {
  16. const std::vector<int> v = { 0, 2, 4, 5};
  17.  
  18. for (int i : {0, 1, 2, 3, 4, 5}) {
  19. std::cout << getIndexFor(v, i) << ":" << i << std::endl;
  20. }
  21. }
  22.  
Success #stdin #stdout 0s 4464KB
stdin
Standard input is empty
stdout
0:0
0:1
1:2
1:3
2:4
3:5