--- id: cpp-stacks title: "C++ Stacks" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["std::stack", "LIFO", "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", "stack", "lifo"] raw_sources: ["https://www.w3schools.com/cpp/cpp_stacks.asp"] applied_in: [] github_commit: "" --- # [[CPP Stacks]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A `stack` is the STL's most restrictive container by design β€” it exposes ONLY `.top()`, deliberately hiding every other element, which is the opposite philosophy from vector's "give me any index" approach; this is a case where LESS access is the feature, because the LIFO guarantee (pancakes added/removed only from the top) is what makes stacks useful for problems like undo-history or call-stack simulation, and that guarantee would be broken if arbitrary access were allowed. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **LIFO (Last In, First Out)** β€” the defining order: the last element pushed is the first one that can be accessed or removed. [S1] - **`stack name`** β€” requires ``; unlike vector/list, a stack CANNOT be initialized with a curly-brace list at declaration time β€” elements must be added via `.push()` after declaring. [S1] - **`.push()`** β€” adds an element to the top of the stack. [S1] - **`.top()`** β€” the ONLY way to read (or, via assignment, change) an element; always refers to the most recently pushed element. [S1] - **`.pop()`** β€” removes the top (most recently added) element; note this differs from vector's `.pop_back()` naming but same "remove from the active end" idea. [S1] - **`.size()` / `.empty()`** β€” same semantics as vector/list. [S1] - **Stacks and Queues are paired concepts** β€” the page explicitly notes queues (FIFO) are the mirror-image structure covered next. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Declaration: `stack cars;` then `cars.push("Volvo"); cars.push("BMW"); cars.push("Ford"); cars.push("Mazda");` β€” resulting order top-to-bottom: Mazda(top), Ford, BMW, Volvo. [S1] - Access/modify: `cars.top()` returns `"Mazda"`; `cars.top() = "Tesla";` changes only the top element. [S1] - Remove: `cars.pop();` removes Mazda, making Ford the new top. [S1] - Attempting `stack cars = {"Volvo", "BMW", "Ford", "Mazda"};` is explicitly called out as NOT allowed β€” a direct contrast to vector/list initialization syntax. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μ„ μ–Έ μ‹œ μ΄ˆκΈ°ν™”κ°€ λΆˆκ°€λŠ₯함**: vector와 listλŠ” `= {...}` μ€‘κ΄„ν˜Έ 리슀트둜 μ„ μ–Έκ³Ό λ™μ‹œμ— μ΄ˆκΈ°ν™”ν•  수 μžˆμ—ˆμ§€λ§Œ, stack은 μ„ μ–Έ ν›„ λ°˜λ“œμ‹œ `.push()`둜만 μš”μ†Œλ₯Ό μ±„μšΈ 수 μžˆλ‹€λŠ” μ œμ•½μ΄ 이번 μ±•ν„°μ—μ„œ vector/listμ™€μ˜ 차이둜 λͺ…μ‹œλ¨. [S1] - **μ ‘κ·Ό κ°€λŠ₯ν•œ μš”μ†Œκ°€ 단 ν•˜λ‚˜(top)둜 μ œν•œλ¨**: vector(인덱슀 전체)Β·list(μ–‘ 끝)에 λΉ„ν•΄ stack은 였직 top μš”μ†Œ ν•˜λ‚˜λ§Œ 읽기/μ“°κΈ° κ°€λŠ₯ν•˜λ‹€λŠ” 점이 LIFO 보μž₯을 μœ„ν•œ μ˜λ„μ  섀계 μ œμ•½μœΌλ‘œ 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” μžλ™μ°¨ 이름을 push()둜 μŒ“κ³  top()으둜 ν™•μΈν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Stack push/top/pop β€” only the top element is ever reachable (C++): ```cpp #include stack cars; cars.push("Volvo"); cars.push("BMW"); cars.push("Ford"); cars.push("Mazda"); // top is now "Mazda" cout << cars.top(); // "Mazda" cars.pop(); // removes "Mazda" cout << cars.top(); // now "Ford" ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP List]], [[CPP Queues]], [[CPP Data Structures]] - **μ°Έμ‘° λ§₯락:** 데이터 ꡬ쑰(STL) μ„Ήμ…˜ β€” Queues 챕터와 짝을 μ΄λ£¨λŠ” 챕터, Queues둜 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Stacks β€” https://www.w3schools.com/cpp/cpp_stacks.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Stacks" page (Astra wiki-curation, P-Reinforce v3.1 format).