DEV Community

Discussion on: Range-based `for` loops

Collapse
 
pauljlucas profile image
Paul J. Lucas

for (int i = 0; i < (int)vector.size(); i++) {

In old code, you should have used size_t i, then there's no need for the cast. Avoid casting if at all possible.

for (auto [key, value] : map) {

It depends on the types of key and value because those are copied by value. For large-ish types, you probably want to do:

for (auto const &[key, value] : map ) {
Enter fullscreen mode Exit fullscreen mode