I have got a (stupid?) C / C++ question about a loop:
for (size_t i = 0; i < std::distance(begin, end); ++i) {
a.push_back(i);
}
begin and end are two iterator. My question is, is std::distance(begin, end) calculated for each element in the loop? Or is it better to use this version:
size_t dist = std::distance(begin, end);
for (size_t i = 0; i < dist; ++i) {
a.push_back(i);
}
Second version is better. In the first one the condition is evaluated each time (no automatic asumption about the result being invariant).