---
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<type> name`** — declared with the element type inside angle brackets, e.g. `vector<string> 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<string> 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>
vector<string> 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).
