fork(1) download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <iterator>
  4. #include <vector>
  5. #include <list>
  6.  
  7. template <typename T, typename V>
  8. T binary_search(T left, T right, V find)
  9. {
  10. if(left > right){
  11. std::swap(left,right);
  12. }
  13.  
  14. if (typeid(*left) == typeid(V)){
  15.  
  16. auto distance = std::distance(left, right);
  17. T current = std::next(left,distance/2);
  18.  
  19. do{
  20. if(*current == find)
  21.  
  22. else if (*current > find){
  23. distance = distance/2;
  24. current = std::prev(current,distance);
  25. }
  26.  
  27. else if (*current < find){
  28. distance = distance/2;
  29. current = std::next(current,distance);
  30. }
  31.  
  32. else return right;
  33.  
  34. }while(distance>0);
  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,10,11,12,13,14,15,16,17,18,19,20};
  42. std::list<int> l = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
  43.  
  44. auto vi = binary_search(l.begin(), l.end(), 12);
  45. int b = *vi;
  46. std::cout << b <<' ';//<< ' ' << std::distance(v.begin(),vi);
  47.  
  48. return 0;
  49. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'T binary_search(T, T, V)':
prog.cpp:22:4: error: expected primary-expression before 'else'
    else if (*current > find){
    ^
prog.cpp: In instantiation of 'T binary_search(T, T, V) [with T = std::_List_iterator<int>; V = int]':
prog.cpp:44:48:   required from here
prog.cpp:10:10: error: no match for 'operator>' (operand types are 'std::_List_iterator<int>' and 'std::_List_iterator<int>')
  if(left > right){
          ^
In file included from /usr/include/c++/5/list:63:0,
                 from prog.cpp:5:
/usr/include/c++/5/bits/stl_list.h:1849:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator>(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
     operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
     ^
/usr/include/c++/5/bits/stl_list.h:1849:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::list<_Tp, _Alloc>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/vector:64:0,
                 from prog.cpp:4:
/usr/include/c++/5/bits/stl_vector.h:1541:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator>(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     ^
/usr/include/c++/5/bits/stl_vector.h:1541:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::vector<_Tp, _Alloc>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:5046:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator>(const _CharT* __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:5046:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   mismatched types 'const _CharT*' and 'std::_List_iterator<int>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:5034:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:5034:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:5022:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:5022:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1119:5: note: candidate: template<class _Iterator> bool std::operator>(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
     operator>(const move_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1119:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::move_iterator<_Iterator>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1113:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator>(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
     operator>(const move_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1113:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::move_iterator<_Iterator>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:360:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator>(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:360:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:310:5: note: candidate: template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator>(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:310:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
  if(left > right){
          ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:233:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/include/c++/5/bits/stl_pair.h:233:5: note:   template argument deduction/substitution failed:
prog.cpp:10:10: note:   'std::_List_iterator<int>' is not derived from 'const std::pair<_T1, _T2>'
  if(left > right){
          ^
stdout
Standard output is empty