Please bear with me if the question is silly.
The following are defined in a header file :
typedef char NAME_T[40];
struct NAME_MAPPING_T
{
NAME_T englishName;
NAME_T frenchName;
};
typedef std::vector<NAME_MAPPING_T> NAMES_DATABASE_T;
Later the need comes that a particular english name be found :
const NAMES_DATABASE_T *wordsDb;
string str;
std::find_if( wordsDb->begin(),
wordsDb->end(),
[str](const NAME_MAPPING_T &m) -> bool { return strncmp(m.englishName, str.c_str(), sizeof(m.englishName)) == 0; } );
This code (which I copy-pasted to be honest) compiles, but if I want to check the value returned by find_if(), like this :
NAMES_DATABASE_T::iterator it;
it = std::find_if(blah ..)
the code will NOT compile !
In effect the line
it = std::find_if(…)
will return the error :
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable conversion)
What is wrong ?
Thanks for your time.
const NAMES_DATABASE_T *wordsDb;Your
wordsDbis const, thereforewordsDb->begin()returns a const iterator, thereforefind_ifreturns a const iterator as well. You’re trying to assign that const iterator to the non-constNAMES_DATABASE_T::iterator it, hence the error.You can use
NAMES_DATABASE_T::const_iteratorto get a const iterator. And you should usestd::stringinstead of those char buffers, unless there are some rare circumstances that require otherwise.