--- id: cpp-sets title: "C++ Sets" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["std::set", "unique sorted elements 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", "set"] raw_sources: ["https://www.w3schools.com/cpp/cpp_sets.asp"] applied_in: [] github_commit: "" --- # [[CPP Sets]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) `set` is the first STL container in this series where insertion ORDER is thrown away entirely โ€” every container so far (vector/list/stack/queue/deque) preserved the order elements were added in, but a set auto-sorts on every insert AND silently drops duplicates, meaning `cars.insert("BMW")` twice is not an error, not a no-op that keeps the old value, but simply invisible โ€” the second insert vanishes with no feedback, which is the opposite of the C mentality where writing the same array slot twice always visibly overwrites it. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **Automatic ascending sort** โ€” elements are always kept in sorted order (alphabetical for strings, numeric for ints), regardless of insertion order. [S1] - **Uniqueness enforced silently** โ€” inserting a duplicate value is simply ignored; no error, no exception, no indication. [S1] - **No index access, no in-place value change** โ€” since order is determined by sorting rather than position, elements cannot be retrieved by `[i]`, and an existing element's VALUE cannot be modified (only added via `.insert()` or removed via `.erase()`). [S1] - **`greater` functor** โ€” passed as a second template parameter (`set> numbers`) to reverse the default ascending sort to descending. [S1] - **`.insert()` / `.erase()` / `.clear()`** โ€” add one element / remove one specific element / remove everything. [S1] - **`.size()` / `.empty()`** โ€” same semantics as other containers. [S1] - **Looping** โ€” for-each only (like list); no indexed loop since there's no index. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Declaration + auto-sort: `set cars = {"Volvo", "BMW", "Ford", "Mazda"};` prints in alphabetical order (BMW, Ford, Mazda, Volvo), NOT insertion order. [S1] - Numeric sets sort numerically: `set numbers = {1, 7, 3, 2, 5, 9};` prints 1,2,3,5,7,9. [S1] - Descending order: `set> numbers = {1, 7, 3, 2, 5, 9};` prints 9,7,5,3,2,1. [S1] - Duplicate handling demonstrated directly: `set cars = {"Volvo", "BMW", "Ford", "BMW", "Mazda"};` still prints only 4 unique elements. [S1] - Add/remove: `cars.insert("Tesla");` / `cars.erase("Volvo");` / `cars.clear();`. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **์‚ฝ์ž… ์ˆœ์„œ๊ฐ€ ์™„์ „ํžˆ ๋ฌด์‹œ๋จ**: ์ง€๊ธˆ๊นŒ์ง€์˜ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ(vector/list/stack/queue/deque)๋Š” ์‚ฝ์ž… ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ–ˆ์ง€๋งŒ, set์€ ๋งค๋ฒˆ ์ž๋™์œผ๋กœ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์žฌ์ ์šฉํ•˜์—ฌ ์‚ฝ์ž… ์ˆœ์„œ ์ž์ฒด๊ฐ€ ์˜๋ฏธ๋ฅผ ์žƒ๋Š”๋‹ค๋Š” ์ ์ด ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ํ™•์ธ๋จ. [S1] - **์ค‘๋ณต ์‚ฝ์ž…์ด ์กฐ์šฉํžˆ ๋ฌด์‹œ๋จ**: C ๋ฐฐ์—ด์—์„œ ๊ฐ™์€ ์ธ๋ฑ์Šค์— ๊ฐ’์„ ๋‘ ๋ฒˆ ์“ฐ๋ฉด ํ•ญ์ƒ ๋ˆˆ์— ๋„๊ฒŒ ๋ฎ์–ด์จ์ง€์ง€๋งŒ, set์— ๊ฐ™์€ ๊ฐ’์„ ๋‘ ๋ฒˆ insert()ํ•˜๋ฉด ์•„๋ฌด ์—๋Ÿฌ๋„ ํ”ผ๋“œ๋ฐฑ๋„ ์—†์ด ๋‘ ๋ฒˆ์งธ ์‚ฝ์ž…์ด ๊ทธ๋ƒฅ ์‚ฌ๋ผ์ง„๋‹ค๋Š” ์ ์ด ํ™•์ธ๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ํ˜„์žฌ ๋ฐœ๊ฒฌ๋œ ์‹ค์ œ ์ ์šฉ ์‚ฌ๋ก€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค โ€” ์ž๋™์ฐจ ์ด๋ฆ„ set์— ์ค‘๋ณต๊ฐ’์„ ๋„ฃ์–ด ์ž๋™์œผ๋กœ ๊ฑธ๋Ÿฌ์ง€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ๊ฐ€ ์›๋ฌธ์—์„œ ์ œ์‹œ๋จ. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) A set auto-sorts and silently drops duplicates on insert (C++): ```cpp #include set cars = {"Volvo", "BMW", "Ford", "BMW", "Mazda"}; // Output (sorted, deduplicated): BMW, Ford, Mazda, Volvo set> numbers = {1, 7, 3, 2, 5, 9}; // Descending via greater: 9, 7, 5, 3, 2, 1 cars.insert("Tesla"); cars.erase("Volvo"); ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.86 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[C++ Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[CPP Deque]], [[CPP Maps]], [[CPP Iterators]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ(STL) ์„น์…˜ โ€” Maps ์ฑ•ํ„ฐ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C++ Sets โ€” https://www.w3schools.com/cpp/cpp_sets.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Sets" page (Astra wiki-curation, P-Reinforce v3.1 format).