As I was doing testing with this in my IDE VS2015 and reading the compiler errors I noticed that Ami Tavory beat me to answering the question. So maybe this can provide some insight or clarity as to what is going on.
In your first search using lower_bound()
it does compile as Ami stated and an iterator to your container is being returned from the search.
In your second search using binary_search()
it does not compile, and as Ami stated it only returns a bool as if it was found or not. As for it not compiling here is the compiler error from Visual Studio 2015 CE
1>------ Build started: Project: LambdaTemplates, Configuration: Debug Win32 ------
1> LambdaTemplates.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(2709): error C2664: 'bool main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>::operator ()(vector_test *,std::string) const': cannot convert argument 1 from 'const std::string' to 'vector_test *'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(2709): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\users\skilz80\documents\visual studio 2015\projects\stackoverflowsolutions\lambdatemplates\lambdatemplates.cpp(46): note: see reference to function template instantiation 'bool std::binary_search<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<vector_test *>>>,std::string,main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>>(_FwdIt,_FwdIt,const _Ty &,_Pr)' being compiled
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<vector_test *>>>,
1> _Ty=std::string,
1> _Pr=main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It says that it can not convert parameter 1 from const std::string
to vector_test*
So what is happening here? Let's abstract away for a moment and lets temporarily write the lambda outside the search function call predicate parameter list. So this section of code would look like this:
auto myLambda = []( vector_test* ptr, std::string name ) {
return name < ptr->get_name();
};
auto it = std::binary_search( test.begin(), test.end(), name, myLambda );
if (it)
std::cout << "It is here\n";
else
std::cout << "It is NOT here\n";
Now lets check out the compiler error:
1>------ Build started: Project: LambdaTemplates, Configuration: Debug Win32 ------
1> stdafx.cpp
1> LambdaTemplates.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(2709): error C2664: 'bool main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>::operator ()(vector_test *,std::string) const': cannot convert argument 1 from 'const std::string' to 'vector_test *'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(2709): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\users\skilz80\documents\visual studio 2015\projects\stackoverflowsolutions\lambdatemplates\lambdatemplates.cpp(45): note: see reference to function template instantiation 'bool std::binary_search<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<vector_test *>>>,std::string,main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>>(_FwdIt,_FwdIt,const _Ty &,_Pr)' being compiled
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<vector_test *>>>,
1> _Ty=std::string,
1> _Pr=main::<lambda_79759dd0460f5d162e02d2bb1cee5db7>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
We get a very similar error message. So lets comment out the line of code for calling the binary search and check the compiler error messages...
auto myLambda = []( vector_test* ptr, std::string name ) {
return name < ptr->get_name();
};
/*auto it = std::binary_search( test.begin(), test.end(), name, myLambda );
if (it)
std::cout << "It is here\n";
else
std::cout << "It is NOT here\n";
*/
And now we don't get any compiler errors. So the lambda itself is fine, however what is happening within the binary_search()
function is this:
You are passing it 2 forward iterators begin
and end
a search term or value name
which is a std::string
. Your forward iterators are vectors of vector_test pointers
. It isn't that your lambda is wrong so-to-speak, it is just that the function can not convert from an std::string
being your search query data type into a vector that contains pointers to vector_test
objects thus making this the wrong type of lambda to use, or the wrong search parameter. Your class of vector_test
objects does not supply any constructors or conversion factors, or overloaded operators to convert to an std::string. Also as a side note when using the binary_search
your container should be pre-sorted.