diff --git a/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md b/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md index a7cb58433..d7853cbaf 100644 --- a/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md +++ b/src/data/roadmaps/cpp/content/references@uUzRKa9wGzdUwwmAg3FWr.md @@ -69,57 +69,13 @@ When iterating over containers such as `std::vector`, the choice of ### Comparison -Loop style +| Loop style | Copies made? | Can modify element? | Efficiency | Typical use case | +|---------------------|--------------|---------------------|------------------------------------|------------------------------------------| +| `auto str` | ✅ Yes | ✅ Only the copy | Less efficient for large objects | When you need a local, mutable copy | +| `auto const &str` | ❌ No | ❌ Read-only | Most efficient for read-only use | Safely read elements without copying | +| `auto &str` | ❌ No | ✅ Modifies original | Efficient, mutates container | Modify elements in place | +| `const auto str` | ✅ Yes | ❌ Read-only | Less efficient for large objects | Explicit read-only copy | -Copies made? - -Can modify element? - -Efficiency - -Typical Use Case - -`auto str` - -✅ Yes - -✅ Only the copy - -Less efficient for large objects - -When you need a local, mutable copy - -`auto const &str` - -❌ No - -❌ Read-only - -Most efficient for read-only use - -Safely read elements without copying - -`auto &str` - -❌ No - -✅ Modifies original - -Efficient, mutates container - -Modify elements in place - -`const auto str` - -✅ Yes - -❌ Read-only - -Less efficient for large objects - -Explicit read-only copy - -* * * Example: Why `const auto str` Can Be Useful ------------------------------------------- @@ -156,4 +112,4 @@ Performance Notes * For **large objects** like `std::string`, `std::vector`, or custom classes, prefer **`auto const &`** to avoid unnecessary copies. * For **small, cheap-to-copy types** like `int`, `char`, or `bool`, using `auto` or `const auto` is fine and often simpler. -* `auto &` should only be used when you _intend_ to modify the elements in place, since it directly mutates the container. \ No newline at end of file +* `auto &` should only be used when you _intend_ to modify the elements in place, since it directly mutates the container.