--- id: cpp-vectors title: "C++ Vectors" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["std::vector", "resizable array C++", "C++ 벑터"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "stl", "vector"] raw_sources: ["https://www.w3schools.com/cpp/cpp_vectors.asp"] applied_in: [] github_commit: "" --- # [[CPP Vectors]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A `vector` is the STL's answer to C's biggest array limitation β€” a fixed size decided at declaration and never changeable afterward β€” by wrapping a resizable buffer behind `.push_back()`/`.pop_back()` while keeping the exact `[]` index syntax C programmers already know, plus adding `.at()` as a bounds-checked alternative that C's raw arrays never offered (C silently reads/writes past the end; C++'s `.at()` throws instead). [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`vector name`** β€” declared with the element type inside angle brackets, e.g. `vector cars;`. [S1] - **Fixed type, dynamic size** β€” the element type cannot change after declaration, but the number of elements can grow or shrink freely, unlike a C array whose size is fixed forever. [S1] - **`[]` vs `.at()`** β€” both index into a vector, but `.at()` is safer because it signals an error when the index is out of range, while `[]` does not. [S1] - **`.push_back()` / `.pop_back()`** β€” add/remove an element at the END of the vector; this is the vector's optimized operation direction. [S1] - **`.front()` / `.back()`** β€” access the first/last element directly, without needing an index. [S1] - **`.size()` / `.empty()`** β€” get element count / check whether the vector has zero elements (`.empty()` returns 1/0, not a named `true`/`false` string). [S1] - **Looping** β€” classic indexed `for` with `.size()`, or the cleaner C++11 for-each (`for (string car : cars)`), or (mentioned, deferred) an iterator. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Declaration with initial values: `vector cars = {"Volvo", "BMW", "Ford", "Mazda"};`. [S1] - Access: `cars[0]`, `cars.front()`, `cars.back()`, `cars.at(1)`. [S1] - Modify: `cars[0] = "Opel";` or the safer `cars.at(0) = "Opel";`. [S1] - Add: `cars.push_back("Tesla");` β€” repeatable for multiple elements. [S1] - Remove: `cars.pop_back();` removes only from the end; for both-ends removal, W3Schools notes a deque is the better fit. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **C λ°°μ—΄μ˜ "크기 κ³ μ •" μ œμ•½μ΄ 사라짐**: C의 배열은 μ„ μ–Έ μ‹œ 크기가 κ³ μ •λ˜μ–΄ μš”μ†Œλ₯Ό μΆ”κ°€/μ‚­μ œν•  수 μ—†μ—ˆμœΌλ‚˜, vectorλŠ” λ™μΌν•œ 인덱슀 문법([])을 μœ μ§€ν•˜λ©΄μ„œλ„ push_back/pop_back으둜 동적 크기 쑰절이 κ°€λŠ₯ν•΄μ‘Œλ‹€λŠ” 점이 이번 μ±•ν„°μ—μ„œ 확인됨. [S1] - **`.at()`의 경계 κ²€μ‚¬λŠ” C 배열에 μ—†λ˜ μ•ˆμ „μž₯치**: CλŠ” λ°°μ—΄ λ²”μœ„λ₯Ό λ²—μ–΄λ‚œ 접근을 컴파일러/λŸ°νƒ€μž„μ΄ 막지 μ•Šκ³  μ •μ˜λ˜μ§€ μ•Šμ€ λ™μž‘(undefined behavior)으둜 λ‚¨κ²¨λ‘μ§€λ§Œ, `.at()`은 λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ λͺ…μ‹œμ μœΌλ‘œ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚¨λ‹€. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” μžλ™μ°¨ 이름을 μ €μž₯ν•˜λŠ” vector에 μ›μ†Œλ₯Ό μΆ”κ°€/μ‚­μ œ/μˆœνšŒν•˜λŠ” μ˜ˆμ œκ°€ 원문 μ „λ°˜μ— 걸쳐 반볡적으둜 μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Core vector operations (C++): ```cpp #include vector cars = {"Volvo", "BMW", "Ford", "Mazda"}; cout << cars.front(); // Volvo cout << cars.at(1); // BMW (bounds-checked) cars.push_back("Tesla"); // add at end cars.pop_back(); // remove from end cout << cars.size(); // element count cout << cars.empty(); // 0 or 1 for (string car : cars) { cout << car << "\n"; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Data Structures]], [[CPP Arrays]], [[CPP List]], [[C Arrays]] - **μ°Έμ‘° λ§₯락:** 데이터 ꡬ쑰(STL) μ„Ήμ…˜ β€” List μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Vectors β€” https://www.w3schools.com/cpp/cpp_vectors.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Vectors" page (Astra wiki-curation, P-Reinforce v3.1 format).