본문으로 건너뛰기 C++ Container Adapters | stack·queue

C++ Container Adapters | stack·queue

C++ Container Adapters | stack·queue

이 글의 핵심

C++ container adapters stack (LIFO), queue (FIFO), priority_queue (heap) usage and practical application. Covers DFS·BFS·Dijkstra algorithm implementation, bracket validation, and task scheduling examples.

Introduction

C++ container adapters are existing containers wrapped with specific interfaces. There are three: stack (LIFO), queue (FIFO), priority_queue (heap). As an analogy, stack is like stacking plates (last placed is removed first), queue is like standing in line (first person in line goes first), priority_queue is like emergency room (person with highest priority gets treated first).

What You’ll Learn

  • Understand usage of stack, queue, priority_queue
  • Grasp time complexity of each adapter
  • Implement DFS, BFS, Dijkstra algorithms
  • Check practical usage patterns and precautions

Container Adapter Overview

Comparison Table

AdapterData StructureInsertDeleteAccessUnderlying Container
stackLIFOpushpoptopdeque (default)
queueFIFOpushpopfront/backdeque (default)
priority_queueHeappushpoptopvector (default)

Practical Implementation

1) stack (LIFO)

#include <iostream>
#include <stack>
int main() {
    std::stack<int> s;
    
    // push
    s.push(1);
    s.push(2);
    s.push(3);
    
    // top
    std::cout << s.top() << std::endl;  // 3
    
    // pop
    s.pop();
    std::cout << s.top() << std::endl;  // 2
    
    // size
    std::cout << s.size() << std::endl;  // 2
    
    // empty
    while (!s.empty()) {
        std::cout << s.top() << " ";
        s.pop();
    }
    std::cout << std::endl;  // 2 1
    
    return 0;
}

2) queue (FIFO)

#include <iostream>
#include <queue>
int main() {
    std::queue<int> q;
    
    // push
    q.push(1);
    q.push(2);
    q.push(3);
    
    // front/back
    std::cout << q.front() << std::endl;  // 1
    std::cout << q.back() << std::endl;   // 3
    
    // pop
    q.pop();
    std::cout << q.front() << std::endl;  // 2
    
    // size
    std::cout << q.size() << std::endl;  // 2
    
    // empty
    while (!q.empty()) {
        std::cout << q.front() << " ";
        q.pop();
    }
    std::cout << std::endl;  // 2 3
    
    return 0;
}

3) priority_queue (Heap)

#include <iostream>
#include <queue>
#include <vector>
int main() {
    // Max heap (default)
    std::priority_queue<int> maxHeap;
    
    maxHeap.push(3);
    maxHeap.push(1);
    maxHeap.push(4);
    maxHeap.push(2);
    
    while (!maxHeap.empty()) {
        std::cout << maxHeap.top() << " ";  // 4 3 2 1
        maxHeap.pop();
    }
    std::cout << std::endl;
    
    // Min heap
    std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
    
    minHeap.push(3);
    minHeap.push(1);
    minHeap.push(4);
    minHeap.push(2);
    
    while (!minHeap.empty()) {
        std::cout << minHeap.top() << " ";  // 1 2 3 4
        minHeap.pop();
    }
    std::cout << std::endl;
    
    return 0;
}

Summary

Key Points

  1. stack: LIFO (Last In First Out)
  2. queue: FIFO (First In First Out)
  3. priority_queue: Heap-based priority queue
  4. Time complexity: stack/queue O(1), priority_queue O(log n)
  5. No iterators: Adapters don’t provide iterators

When to Use

Use stack when:

  • DFS (Depth-First Search)
  • Bracket/parenthesis validation
  • Undo/redo functionality
  • Expression evaluation ✅ Use queue when:
  • BFS (Breadth-First Search)
  • Task queue
  • Message queue
  • Level-order traversal ✅ Use priority_queue when:
  • Dijkstra’s algorithm
  • Task scheduling by priority
  • Event simulation
  • Top K elements

Best Practices

  • ✅ Check empty() before top()/front()
  • ✅ Use appropriate underlying container
  • ✅ Use custom comparator for priority_queue
  • ❌ Don’t access by index (no random access)
  • ❌ Don’t iterate (no iterators provided)


Frequently Asked Questions (FAQ)

Q. When would I use this in practice?

A. C++ container adapters complete guide.

Q. What should I read before this?

A. Follow the previous article or related articles links at the bottom of each post to learn in sequence. See the C++ series index for the full picture.

Q. Where can I study this more deeply?

A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.


Other articles related to this topic.


Keywords Covered in This Article (Related Search Terms)

This article covers C++, container, stack, queue, priority_queue, STL.