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_iterator
s, not std::vector<int>::iterator
s. 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
Post a Comment