C++ vector vs list vs deque — Complete Guide
이 글의 핵심
Complete comparison guide for C++ STL containers vector, list, deque. Cache efficiency secrets not revealed by time complexity alone, actual benchmark results, performance differences by element count, practical cases in game/web/system programming. Proves with data why vector is faster than list even with frequent middle insertions.
Introduction: “vector, list, deque…Which Should I Use?"
"They said use list for frequent middle insertions but it’s slower”
C++ STL provides three main sequence containers: vector, list, and deque. Each has different time complexity and memory layout, so choosing appropriately for the situation is important.
To use an analogy, vector is like an auditorium with continuous seats, list is like a queue easy to insert into (connected by pointers), and deque is like a crosswalk easy to extend front and back. For mostly sequential access, the auditorium (vector) is often more cache-friendly.
When to use vector, when list·deque?
| Aspect | vector | list | deque |
|---|---|---|---|
| Performance | Sequential access·cache excellent | Middle insertion theoretically O(1) but high constant·allocation cost | Both ends insertion·deletion O(1) |
| Usability | [], reserve etc intuitive | Node-based iterator invalidation rules need attention | Index access more restricted than vector |
| Application Scenario | Default choice | When insertion position is truly random and frequent | Sliding window·double-ended queue |
Common misconceptions:
- “Always use list for frequent middle insertions” → Wrong (need to consider cache efficiency)
- “vector is slow because it’s an array” → Wrong (fastest in most cases)
- “deque is only fast for both-end insertion” → Partially correct (middle access also O(1))
What this guide covers:
- Internal structure of vector, list, deque
- Time complexity comparison (insertion, deletion, lookup)
- Actual performance benchmarks (including cache efficiency)
- Container selection guide by situation
- Memory usage comparison
1. Container Internal Structure
vector: Contiguous Memory Array
[1][2][3][4][5][6][7][8]...
↑ ↑
begin end
Characteristics:
- Contiguous memory block
- Highest cache efficiency
- Entire copy on reallocation
std::vector<int> vec = {1, 2, 3, 4, 5};
// Memory layout
// [1][2][3][4][5][capacity spare space...]
list: Doubly Linked List
[1] ⇄ [2] ⇄ [3] ⇄ [4] ⇄ [5]
↑ ↑
begin end
Characteristics:
- Nodes scattered in memory
- Each node has prev/next pointers
- No reallocation
std::list<int> lst = {1, 2, 3, 4, 5};
// Memory layout (conceptual)
// Node1: [prev=null][data=1][next=Node2]
// Node2: [prev=Node1][data=2][next=Node3]
// ...
deque: Chunk Array
Chunk1: [1][2][3][4]
Chunk2: [5][6][7][8]
Chunk3: [9][10][11][12]
↑
map (chunk pointer array)
Characteristics:
- Multiple fixed-size chunks
- Both-end insertion O(1)
- Middle access O(1) (slightly slower)
2. Time Complexity Comparison
Time Complexity by Operation
| Operation | vector | list | deque |
|---|---|---|---|
| Front insertion | O(n) | O(1) | O(1) |
| Back insertion | O(1) amortized | O(1) | O(1) |
| Middle insertion | O(n) | O(1) | O(n) |
| Front deletion | O(n) | O(1) | O(1) |
| Back deletion | O(1) | O(1) | O(1) |
| Middle deletion | O(n) | O(1) | O(n) |
| Index access | O(1) | O(n) | O(1) |
| Sequential traversal | Fast | Slow | Medium |
| Memory efficiency | High | Low | Medium |
Note: Time Complexity ≠ Actual Performance
Cache efficiency greatly affects actual performance.
// list: O(1) insertion
std::list<int> lst;
for (int i = 0; i < 1000000; ++i) {
lst.push_back(i); // O(1), but many cache misses
}
// vector: O(1) amortized insertion
std::vector<int> vec;
vec.reserve(1000000);
for (int i = 0; i < 1000000; ++i) {
vec.push_back(i); // O(1), high cache efficiency
}
// Actual performance: vector is 10x faster!
Understanding with everyday analogy: Time complexity can be compared to finding a desired page in a book. O(1) is finding directly from table of contents, O(log n) is binary search judging front/back by opening middle, O(n) is turning pages one by one from first, O(n²) is checking all page combinations.
3. Actual Performance Benchmarks
Benchmark Environment
CPU: Intel i7-12700K
RAM: 32GB DDR4-3200
Compiler: GCC 11.2, -O3
Element count: 1 million
Test 1: Back Insertion (push_back)
// Benchmark code
template <typename Container>
void benchPushBack() {
Container c;
for (int i = 0; i < 1000000; ++i) {
c.push_back(i);
}
}
Results:
| Container | Time | Relative Speed |
|---|---|---|
| vector | 12ms | 1.0x (baseline) |
| vector (reserve) | 8ms | 0.67x (faster) |
| list | 180ms | 15x (slower) |
| deque | 25ms | 2.1x (slower) |
Analysis: vector is overwhelmingly fast (cache efficiency).
Test 2: Front Insertion (push_front)
template <typename Container>
void benchPushFront() {
Container c;
for (int i = 0; i < 100000; ++i) { // 100k (1M too slow)
c.push_front(i);
}
}
Results:
| Container | Time | Relative Speed |
|---|---|---|
| vector | 12,000ms | 1000x (very slow) |
| list | 18ms | 1.5x |
| deque | 12ms | 1.0x (fastest) |
Analysis: For front insertion, deque is best.
Test 3: Middle Insertion
template <typename Container>
void benchMiddleInsert() {
Container c;
// Initial data
for (int i = 0; i < 10000; ++i) {
c.push_back(i);
}
// Insert 1000 times in middle
auto it = c.begin();
std::advance(it, 5000); // Middle position
for (int i = 0; i < 1000; ++i) {
c.insert(it, 999);
}
}
Results:
| Container | Time | Relative Speed |
|---|---|---|
| vector | 45ms | 2.25x |
| list | 20ms | 1.0x (fastest) |
| deque | 50ms | 2.5x |
Analysis: For middle insertion, list is advantageous (but vector is competitive with fewer elements).
Test 4: Sequential Traversal
template <typename Container>
void benchIteration(const Container& c) {
long long sum = 0;
for (int x : c) {
sum += x;
}
}
Results (1M elements):
| Container | Time | Relative Speed |
|---|---|---|
| vector | 2ms | 1.0x (baseline) |
| list | 25ms | 12.5x (slower) |
| deque | 5ms | 2.5x (slower) |
Analysis: For sequential traversal, vector is overwhelmingly fast (cache efficiency).
4. Selection Guide by Situation
When to Use vector
✅ Use vector when:
- Sequential access is frequent
- Random access needed
- Memory efficiency important
- Default choice
❌ Don’t use vector when:
- Front insertion/deletion very frequent
- Both-end access needed
When to Use list
✅ Use list when:
- Middle insertion/deletion very frequent
- Need to maintain iterators after insertion/deletion
- Don’t need random access
❌ Don’t use list when:
- Sequential access frequent (cache inefficient)
- Memory overhead matters
- Element count small (< 1000)
When to Use deque
✅ Use deque when:
- Both-end insertion/deletion frequent
- Need queue or double-ended queue
- Need sliding window
❌ Don’t use deque when:
- Only back insertion needed (use vector)
- Memory layout must be contiguous
Summary
Key Points
- vector: Default choice, fastest in most cases
- list: Only for very frequent middle insertion/deletion
- deque: For frequent both-end insertion/deletion
- Cache efficiency: More important than time complexity
- Benchmark: Always measure actual performance
Decision Flowchart
Need front insertion/deletion?
├─ Yes → deque
└─ No
└─ Need frequent middle insertion?
├─ Yes (10000+ elements) → list
└─ No → vector (default)
Practical Tips
- ✅ Use vector as default
- ✅ Use reserve to prevent reallocation
- ✅ Verify with benchmarks
- ❌ Don’t choose based on time complexity alone
- ❌ Don’t use list for small element counts
Related Articles
- C++ vector Basics
- C++ Container Selection
- C++ array vs vector
Master container selection for optimal C++ performance! 🚀
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Complete comparison guide for C++ STL containers vector, list, deque. Cache efficiency secrets not revealed by time comp… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ vector 기초 완벽 가이드 | 초기화·연산·용량 관리와 실전 패턴
- C++ 컨테이너 선택 가이드 | vector/list/deque/map/set 상황별 선택과 성능 최적화
- C++ 배열 vs vector | ‘어느 게 나을까?’ 성능과 안전성 비교
이 글에서 다루는 키워드 (관련 검색어)
C++, STL, vector, list, deque, container, performance 등으로 검색하시면 이 글이 도움이 됩니다.