fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. typedef std::vector<int> vect_t;
  5. typedef std::vector<int>::iterator vect_iter;
  6.  
  7. vect_iter bsearch (vect_t& v, int value)
  8. {
  9. auto left = v.begin();
  10. auto right = v.end();
  11. while (left != right)
  12. {
  13. auto mid = left + (right - left) / 2;
  14. if (*mid == value)
  15. return mid;
  16. else if (value > *mid)
  17. left = mid + 1;
  18. else
  19. right = mid;
  20. }
  21. return v.end();
  22. }
  23.  
  24. vect_iter recursive_bsearch (vect_iter& left, vect_iter& right, int value)
  25. {
  26. if (left != right)
  27. {
  28. auto mid = left + (right - left) / 2;
  29. if (*mid == value)
  30. return mid;
  31. else if (value > *mid)
  32. return recursive_bsearch (++mid, right, value);
  33. else
  34. return recursive_bsearch (left, --mid, value);
  35. }
  36. return right;
  37. }
  38.  
  39. int main()
  40. {
  41. std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  42.  
  43. std::cout << *bsearch(v, 7) << std::endl;
  44. std::cout << *bsearch(v, 3) << std::endl;
  45. std::cout << *bsearch(v, 11) << std::endl;
  46.  
  47. std::cout << *recursive_bsearch(v.begin(), v.end(), 7) << std::endl;
  48. std::cout << *recursive_bsearch(v.begin(), v.end(), 3) << std::endl;
  49. std::cout << *recursive_bsearch(v.begin(), v.end(), 11) << std::endl;
  50.  
  51. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:47:41: error: invalid initialization of non-const reference of type 'vect_iter& {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >&}' from an rvalue of type 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}'
  std::cout << *recursive_bsearch(v.begin(), v.end(), 7) << std::endl;
                                         ^
prog.cpp:24:11: note:   initializing argument 1 of 'vect_iter recursive_bsearch(vect_iter&, vect_iter&, int)'
 vect_iter recursive_bsearch (vect_iter& left, vect_iter& right, int value)
           ^
prog.cpp:48:48: error: invalid initialization of non-const reference of type 'vect_iter& {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >&}' from an rvalue of type 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}'
         std::cout << *recursive_bsearch(v.begin(), v.end(), 3) << std::endl;
                                                ^
prog.cpp:24:11: note:   initializing argument 1 of 'vect_iter recursive_bsearch(vect_iter&, vect_iter&, int)'
 vect_iter recursive_bsearch (vect_iter& left, vect_iter& right, int value)
           ^
prog.cpp:49:48: error: invalid initialization of non-const reference of type 'vect_iter& {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >&}' from an rvalue of type 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}'
         std::cout << *recursive_bsearch(v.begin(), v.end(), 11) << std::endl;
                                                ^
prog.cpp:24:11: note:   initializing argument 1 of 'vect_iter recursive_bsearch(vect_iter&, vect_iter&, int)'
 vect_iter recursive_bsearch (vect_iter& left, vect_iter& right, int value)
           ^
stdout
Standard output is empty