C ++: binary compilation 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 made #include <algorithm> at the beginning of the file, and I cannot understand the error. The following are the containers used in a function call:

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

Thanks.

+4
source share
5 answers

To use binary_search , enter the siquence value, which must be sorted according to a specific comparison predicate. Later, the same matching predicate must be set (explicitly or implicitly) to binary_search , which will be used during the search.

So, the questions you should answer in this case are the following

  • Sort input sequence? If not, you can stop right here. binary_search cannot be used with unordered sequences.
  • If it is sorted, then what comparison predicate was used to sort it? And how was this passed to the sort function?

Once you know the predicate of comparison and the approach of passing, you can do the same with binary_search .

Note that comparison is not necessarily implemented via operator < , as other answers might suggest. For example, it might be a stand-alone functor-based comparison predicate. Moreover, the fact that binary_search did not take an automatic comparison predicate (as in the case of operator < ) offers a "stand-alone" approach.

+2
source

To use binary search, your products must be comparable. R3Point has no built-in comparison, this is the main reason.

In addition, to use binary_search your list must already be sorted compared to the comparison operation.

+3
source

You need to implement operator < for your R3Point class. The binary_search() function will use this operator to determine how to find the target.

+3
source

std::binary_search uses the predicate function to compare records. This is operator < by default, so you need to overload this op for R3Point .

Keep in mind that the input range must be ordered by this statement for std::binary_search to work correctly (well, that is the nature of binary search).

See http://www.sgi.com/tech/stl/binary_search.html .

+3
source

If R3Point is implemented by you, you can add operator< for it.

Otherwise, you must implement the comparison functor and assign it binary_search .

Remember the following mark :

Returns true if for an element in the range [first,last) value is equivalent , and false otherwise.

0
source

Source: https://habr.com/ru/post/1302636/


All Articles