Since C++11
there is a cool way to iterate through the containers using for range loop. This version is recommended by lots of C++
gurus such as Herb Sutter, Scott Meyers, Jason Turner, etc. But suppose we need to extract the indices of the certain value of std::vector
, so in that case what code snippet is preferable overall, taking into account all main criteria of C++
good coding(readability, efficiency, memory, etc.)?
Version 1:
for(size_t id = 0; id < myVec.size(); ++id) {
//extracting indices with a certain value
}
or
Version 2:
{
size_t id = 0;
for(auto& elem : myVec) {
//extracting indices with a certain value
++id;
}
}