fork(7) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int getIndex(const std::vector<int>& V, int val)
  7. {
  8. auto iter = std::upper_bound(V.begin(), V.end(), val);
  9. if ( iter != V.begin())
  10. return std::distance(V.begin(), std::prev(iter));
  11. return 0;
  12. }
  13.  
  14.  
  15. int main()
  16. {
  17. std::vector<int> V = {5,7,10,12};
  18. std::vector<int> V2 = {7, 8, 10, 11, 13};
  19. std::for_each(V2.begin(), V2.end(), [&](int val)
  20. {std::cout << getIndex(V, val) << " ";});
  21. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1 1 2 2 3