c++ - how to return index of element in vector -
given vector
vector<classx *> myvec;
how return index i
of 1 of elements in following function;
size_t nearestelement(classy const& p){ size_t i(0); double d = distance(myvec[i]->position(), p); (auto const& element : myvec){ if(distance(element->position(), p) < d){ = ???; // index of current element } return i; } }
where position()
function defined in classx
, distance
not std::distance
function, function defined self.
change range based regular for, or add indexing variable current for:
int index = 0; (auto const& element : myvec){ if(distance(element->position(), p) < d){ = index; // index of current element } index++ ...
Comments
Post a Comment