Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: binary search compile error

I have the following lines of code:

if(std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[0]) &&
         std::binary_search(face_verts.begin(), face_verts.end(), left_right_vert[1]))

And when I compile my code, I get the following errors:

In file included from /usr/include/c++/4.4/algorithm:62,
                 from R3Mesh.cpp:10:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<R3Point*, std::vector<R3Point, std::allocator<R3Point> > >, _Tp = R3Point]’:
R3Mesh.cpp:1335:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2762: error: no match for ‘operator<’ in ‘__val < __i.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = R3Point*, _Container = std::vector<R3Point, std::allocator<R3Point> >]()’
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_FIter std::lower_bound(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<R3Point*, std::vector<R3Point, std::allocator<R3Point> > >, _Tp = R3Point]’:
/usr/include/c++/4.4/bits/stl_algo.h:2761:   instantiated from ‘bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<R3Point*, std::vector<R3Point, std::allocator<R3Point> > >, _Tp = R3Point]’
R3Mesh.cpp:1335:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:2442: error: no match for ‘operator<’ in ‘__middle.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = R3Point*, _Container = std::vector<R3Point, std::allocator<R3Point> >]() < __val’
make: *** [R3Mesh.o] Error 1

I did #include <algorithm> in the beginning of the file and I can't seem to figure out the error. The following are the containers used in the function call:

vector <R3Point > face_verts;
vector <R3Point > left_right_vert;

Thanks.

like image 548
Myx Avatar asked Dec 23 '22 05:12

Myx


1 Answers

In order to use binary search, your items must be comparable. R3Point doesn't have built-in comparison, that's the core reason.

Moreover, for using binary_search your list must be already sorted wrt the comparison operation.

like image 154
Vlad Avatar answered Jan 05 '23 15:01

Vlad