--- id: cpp-maps title: "C++ Maps" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["std::map", "key-value pairs C++", "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", "map"] raw_sources: ["https://www.w3schools.com/cpp/cpp_maps.asp"] applied_in: [] github_commit: "" --- # [[CPP Maps]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `map` reuses set's "auto-sort + unique" behavior but shifts what must be unique from the ELEMENT itself to just the KEY, letting VALUES repeat freely β€” and it overloads `[]` with a dual meaning no other container has: `people["Jenny"] = 22;` on a key that doesn't exist SILENTLY CREATES it (unlike vector's `[]`, which never grows the container), meaning map's `[]` is simultaneously a read, a write, AND an insert operator depending on context, a three-way overload that requires `.at()` or `.count()` when you need to tell those cases apart. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`map name`** β€” two type parameters instead of one, e.g. `map people;`; requires ``. [S1] - **Unique keys, non-unique values** β€” like set's uniqueness rule but applied only to the key half of each pair; values can repeat freely. [S1] - **Auto-sorted by key** β€” same ascending-order-by-default behavior as set, reversible with `greater` as the third template parameter. [S1] - **`[]` as read/write/insert** β€” `people["John"]` reads the value for an existing key, `people["John"] = 50;` overwrites it, and `people["Jenny"] = 22;` on a NEW key silently inserts it β€” three distinct behaviors under one syntax. [S1] - **`.at()`** β€” safer read/write alternative that throws if the key doesn't exist, unlike `[]` which would silently create a new entry instead of erroring. [S1] - **`.insert({key, value})`** β€” explicit alternative to `[]` for adding; if the key already exists, the insert is silently ignored (first value wins, matching set's duplicate-drop behavior). [S1] - **`.count(key)`** β€” returns 1/0 to check key existence without risking an accidental insert. [S1] - **Looping requires `auto` + `.first`/`.second`** β€” `for (auto person : people) { cout << person.first << person.second; }`, since each element is a key-value pair, not a single value. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Declaration: `map people = { {"John", 32}, {"Adele", 45}, {"Bo", 29} };`. [S1] - Access: `people["John"]`, `people.at("Adele")`; `.at()` throws for a missing key (`people.at("Jenny")` errors), while `people["Jenny"]` would just create it. [S1] - Duplicate-key insert is ignored: `people.insert({"Jenny", 22}); people.insert({"Jenny", 30});` keeps only 22. [S1] - Existence check without mutation: `people.count("John")` returns 1. [S1] - Loop pattern: `for (auto person : people) { cout << person.first << " is: " << person.second << "\n"; }` β€” output is sorted by key (Adele, Bo, John), not insertion order. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **[] μ—°μ‚°μžκ°€ vector와 λ‹€λ₯΄κ²Œ λ™μž‘ν•¨**: vector의 []λŠ” μ‘΄μž¬ν•˜λŠ” 인덱슀만 읽고 λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ μ •μ˜λ˜μ§€ μ•Šμ€ λ™μž‘μ΄μ—ˆμ§€λ§Œ, map의 []λŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 킀에 값을 λŒ€μž…ν•˜λ©΄ 쑰용히 μƒˆ μš”μ†Œλ₯Ό μƒμ„±ν•œλ‹€λŠ” 점이 vectorμ™€μ˜ 핡심 차이둜 확인됨. [S1] - **set의 κ³ μœ μ„± κ·œμΉ™μ΄ ν‚€μ—λ§Œ μ μš©λ˜λ„λ‘ 완화됨**: set은 μš”μ†Œ 전체가 κ³ μœ ν•΄μ•Ό ν–ˆμ§€λ§Œ, map은 ν‚€λ§Œ κ³ μœ ν•˜λ©΄ 되고 값은 자유둭게 쀑볡될 수 μžˆλ‹€λŠ” 점이 set κ·œμΉ™μ˜ 뢀뢄적 μ™„ν™”λ‘œ 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ‚¬λžŒ 이름과 λ‚˜μ΄λ₯Ό μ €μž₯ν•˜λŠ” mapμ—μ„œ 값을 읽고, μˆ˜μ •ν•˜κ³ , .count()둜 쑴재 μ—¬λΆ€λ₯Ό ν™•μΈν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Map's `[]` reads, writes, AND silently inserts depending on context (C++): ```cpp #include map people = { {"John", 32}, {"Adele", 45}, {"Bo", 29} }; people["Jenny"] = 22; // key doesn't exist -> silently inserted cout << people.at("Adele"); // safe read, throws if missing cout << people.count("John"); // 1 if exists, 0 if not -- no risk of insert for (auto person : people) { // sorted by key: Adele, Bo, Jenny, John cout << person.first << " is: " << person.second << "\n"; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Sets]], [[CPP Iterators]], [[CPP Data Structures]] - **μ°Έμ‘° λ§₯락:** 데이터 ꡬ쑰(STL) μ„Ήμ…˜ β€” Iterators μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Maps β€” https://www.w3schools.com/cpp/cpp_maps.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Maps" page (Astra wiki-curation, P-Reinforce v3.1 format).