c++ - Finding Values index position in vector -


i looking find index of searched value in array code. getting error,which has constant vector, not sure how fix it.

int linearfind( const vector<int>& vec, int y){     vector<int>::iterator t=find(vec.begin(), vec.end(), y);     if (t != vec.end())         return (t-vec.begin());     else         return -1; } 

the problem that, vec passed const&, iterators returned begin , end std::vector<int>::const_iterators, not std::vector<int>::iterators. thus, find return std::vector<int>::const_iterator cannot converted std::vector<int>iterator drop const.

so solve this, either use

std::vector<int>::const_iterator t = find(vec.begin(), vec.end(), y); 

of, if use c++11 or later, easier

auto t = find(vec.begin(), vec.end(), y); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -