--- id: cpp-iterators title: "C++ Iterators" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["begin() end()", "vector iterator", "C++ ๋ฐ˜๋ณต์ž"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "stl", "iterator"] raw_sources: ["https://www.w3schools.com/cpp/cpp_iterators.asp"] applied_in: [] github_commit: "" --- # [[CPP Iterators]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) An iterator is a pointer with a container-agnostic interface โ€” `*it` dereferences and `++it` advances exactly like a raw C pointer walking an array, but the SAME `begin()`/`end()`/`*it`/`++it` syntax works identically across vector, list, deque, set, AND map, which is precisely the capability C's raw pointers never had (a `int*` walking an array shares no interface with a linked-list traversal via `node->next`) โ€” and this uniform interface is *why* for-each loops and generic algorithms like `sort()`/`find()` can operate on any container without being rewritten per container type. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **Iterator = a "pointer" to an element** โ€” used to access/iterate through data structures by pointing to elements rather than copying them. [S1] - **`begin()` / `end()`** โ€” functions belonging to the CONTAINER (not the iterator) that return an iterator to the first element / to one-past-the-last element; `end()` never points to a real element, only marks the stopping condition. [S1] - **`*it`** โ€” dereference operator, accesses the value the iterator currently points to (and can assign through it: `*it = "Tesla";` modifies the container in place). [S1] - **`++it`** โ€” advances the iterator to the next element. [S1] - **`auto` keyword** โ€” lets the compiler infer the verbose iterator type (`vector::iterator`) automatically; works in the loop declaration itself. [S1] - **For-each vs. iterator** โ€” for-each is simpler for read-only traversal; iterators are required when you need to modify, erase, iterate in reverse, or skip elements during the loop. [S1] - **`.erase(it)` inside a loop** โ€” removing an element mid-iteration returns the NEXT valid iterator, which is why the erase-loop pattern reassigns `it = cars.erase(it);` instead of also calling `++it`. [S1] - **`rbegin()` / `rend()`** โ€” reverse-order counterparts to `begin()`/`end()`, for iterating backward. [S1] - **Iterator support is not universal** โ€” vector, list, deque, map, and set support iterators; stack and queue do NOT. [S1] - **Iterators power `` functions** โ€” `sort()`, `find()`, etc. take a begin/end iterator pair as parameters rather than the container itself. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Basic loop: `vector::iterator it; for (it = cars.begin(); it != cars.end(); ++it) { cout << *it; }`. [S1] - `auto` shorthand: `for (auto it = cars.begin(); it != cars.end(); ++it) { cout << *it; }`. [S1] - Erase-during-iteration pattern: `for (auto it = cars.begin(); it != cars.end(); ) { if (*it == "BMW") { it = cars.erase(it); } else { ++it; } }` โ€” no `++it` in the header since erase already advances. [S1] - Reverse iteration: `for (auto it = cars.rbegin(); it != cars.rend(); ++it) { cout << *it; }`. [S1] - Map iteration needs `->first`/`->second` (arrow, since `it` is a pointer-like object to a pair): `for (auto it = people.begin(); it != people.end(); ++it) { cout << it->first << it->second; }`. [S1] - `sort(cars.begin(), cars.end());` from `` โ€” the same begin/end pair used for manual loops is reused as the algorithm's range argument. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **C์˜ ์›์‹œ ํฌ์ธํ„ฐ์—๋Š” ์—†๋˜ ํ†ต์ผ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋„์ž…๋จ**: C์—์„œ ๋ฐฐ์—ด์€ ํฌ์ธํ„ฐ ์‚ฐ์ˆ (`arr + i`)๋กœ, ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” `node->next`๋กœ ์ˆœํšŒ ๋ฐฉ์‹์ด ์™„์ „ํžˆ ๋‹ฌ๋ž์ง€๋งŒ, C++ iterator๋Š” vector/list/deque/set/map ๋ชจ๋‘์—์„œ ๋™์ผํ•œ `begin()`/`end()`/`*it`/`++it` ๋ฌธ๋ฒ•์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ด ํ•ต์‹ฌ ์ฐจ์ด๋กœ ํ™•์ธ๋จ. [S1] - **stack/queue๋Š” iterator๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Œ**: vector/list/deque/map/set๊ณผ ๋‹ฌ๋ฆฌ, ์ ‘๊ทผ ์ง€์ ์ด ์˜๋„์ ์œผ๋กœ ์ œํ•œ๋œ stack๊ณผ queue๋Š” iterator ์ž์ฒด๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด ๋ช…์‹œ์ ์œผ๋กœ ํ™•์ธ๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) vector์—์„œ ํŠน์ • ๊ฐ’("BMW")์„ ๊ฐ€์ง„ ์š”์†Œ๋ฅผ ์ˆœํšŒ ์ค‘์— ์‚ญ์ œํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ iterator๊ฐ€ for-each๋ณด๋‹ค ์œ ๋ฆฌํ•œ ์‹ค์ „ ์‚ฌ๋ก€๋กœ ์›๋ฌธ์—์„œ ์ œ์‹œ๋จ. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Iterator-based erase during traversal โ€” impossible with a simple for-each (C++): ```cpp #include vector cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (auto it = cars.begin(); it != cars.end(); ) { if (*it == "BMW") { it = cars.erase(it); // erase returns the next valid iterator } else { ++it; } } ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.86 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[C++ Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[CPP Maps]], [[CPP Algorithms]], [[CPP Pointers]], [[CPP Auto]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ(STL) ์„น์…˜ โ€” Algorithms ์ฑ•ํ„ฐ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C++ Iterators โ€” https://www.w3schools.com/cpp/cpp_iterators.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Iterators" page (Astra wiki-curation, P-Reinforce v3.1 format).