What's the recommended way of iterating a container in C11?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In C++11, the recommended way to iterate a container is usually the range-based for loop. It is shorter, less error-prone than manual iterator loops, and makes your intent clearer, especially when combined with const auto& for read-only iteration.
Prefer Range-Based for For Ordinary Traversal
If you simply want to visit each element in order, use the range-based loop:
This style has several advantages:
- less boilerplate
- no explicit iterator type
- fewer off-by-one mistakes
- clearer read-only intent
For most loops, that is enough.
Choose Value, Reference, Or const Reference Carefully
The loop variable matters just as much as the loop syntax.
Use const auto& when you only need to read:
Use auto& when you want to modify elements in place:
Use plain auto only when copying is intentional or inexpensive:
This distinction matters because range-based loops can silently copy expensive objects if you omit the reference.
Use Iterators When You Need More Control
The old iterator form is still the right tool when you need:
- erasure during iteration
- access to the iterator itself
- movement that is not simple linear traversal
Example of safe erasure:
Trying to do that with a range-based loop is awkward and often incorrect because erasing invalidates the loop's internal traversal state.
Index Loops Still Have A Place
If you need the numeric position, use an index loop deliberately:
Do not force an index loop when you do not need the index. It adds noise and can be less generic because not all containers support random access.
Algorithms Are Often Better Than Loops
Sometimes the best iteration style is not a loop at all. If your goal is to apply a standard operation, the algorithm library expresses intent better.
In C++11, range-based for is often more readable than std::for_each, but the bigger lesson remains: choose the construct that matches the operation, not the one you happen to remember first.
A Practical Rule Of Thumb
For everyday C++11 code:
- Use range-based
forfor straightforward traversal. - Use
const auto&by default for read-only access. - Use
auto&when modifying elements. - Use iterators when erasing or when traversal logic is special.
- Use indices only when the position matters.
That covers most real container iteration decisions.
Common Pitfalls
The most common mistake is writing for (auto item : container) and accidentally copying every element. With large strings, vectors, or custom objects, that can be an unnecessary performance hit.
Another mistake is using indexing on containers that are not random-access, such as std::list. A range-based loop is more generic and usually more idiomatic there.
People also try to erase elements inside a range-based loop. That often leads to invalidated iterators or subtle bugs. Use an explicit iterator loop when the container may change.
Finally, do not confuse "modern" with "always shortest." Range-based loops are usually best, but the right choice still depends on whether you need mutation, erasure, or index information.
Summary
- In C++11, range-based
foris the default recommendation for normal container iteration. - '
const auto&is usually the best read-only loop variable choice.' - Use
auto&to modify elements and plainautoonly when copying is intentional. - Fall back to iterators when erasing or when traversal needs more control.
- Use index-based loops only when the numeric position is actually part of the task.
Related reading
- What''s the target group port for, when using Application Load Balancer EC2 Container Service
- When does Docker image cache invalidation occur?
- When not to use docker run --init
- When to pull from Docker repo and when from Git repo and then build?
- When to use Docker HEALTHCHECK vs livenessProbe / readinessProbe
- Where are Docker images stored on the host machine?
- Where are Docker images stored on the host machine?
- Where can I find the sha256 code of a docker image?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.