Container adapters in C++ are specialized containers that provide a restricted interface over existing STL containers. They adapt existing containers to support specific access patterns and behaviors.
- Built on top of containers such as deque, vector, and list.
- Used to implement data structures like stacks, queues, and priority queues.
Types of Container Adapters
The C++ STL provides three container adapters:
Stack in C++
A stack follows the Last In First Out (LIFO) principle. Elements can only be inserted and removed from the top. Common Operations include:
| Function | Description |
|---|---|
| push(x) | Inserts an element at the top |
| pop() | Removes the top element |
| top() | Returns the top element |
| empty() | Checks whether the stack is empty |
| size() | Returns the number of elements |
#include <iostream>
#include <stack>
using namespace std;
int main()
{
// Declaring an instance of the stack adapter with
stack<int> myStack;
// Push elements onto the stack
myStack.push(10);
myStack.push(20);
myStack.push(30);
// print size of stack
cout << "Size of stack is: " << myStack.size() << endl;
// Pop elements from the stack until it's empty
cout << "Elements in a stack are: ";
while (!myStack.empty()) {
// Display the top element
cout << myStack.top() << " ";
// Remove the top element
myStack.pop();
}
return 0;
}
Output
Size of stack is: 3 Elements in a stack are: 30 20 10
Time Complexity: O(n)
Auxilliary Space: O(1)
Queue in C++
A queue follows the First In First Out (FIFO) principle. Elements are inserted at the back and removed from the front.
By default, queue uses deque as its underlying container. Common Operations include:
| Function | Description |
|---|---|
| push(x) | Inserts an element at the back |
| pop() | Removes the front element |
| front() | Returns the first element |
| back() | Returns the last element |
| empty() | Checks whether the queue is empty |
| size() | Returns the number of elements |
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Create a queue of integers
queue<int> myQueue;
// Add elements to the queue
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
// Print the size of the queue
cout << "Size of queue is: " << myQueue.size() << endl;
// Print the elements in the queue
cout << "Elements in a queue are: ";
while (!myQueue.empty()) {
// Print the front element of the queue
cout << myQueue.front() << " ";
// Remove the front element from the queue
myQueue.pop();
}
return 0;
}
Output
Size of queue is: 3 Elements in a queue are: 10 20 30
Time Complexity: O(n)
Auxilliary Space: O(1)
Priority Queue in C++
- A priority_queue stores elements according to their priority instead of insertion order.
- By default, it behaves as a max heap, meaning the largest element always remains at the top.
- It uses vector as the default underlying container.
Common Operations include:
| Function | Description |
|---|---|
| push(x) | Inserts an element |
| pop() | Removes the highest-priority element |
| top() | Returns the highest-priority element |
| empty() | Checks whether the priority queue is empty |
| size() | Returns the number of elements |
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Create a priority queue of integers (default is a
// max-heap)
priority_queue<int> myPriorityQueue;
// Add elements to the priority queue
myPriorityQueue.push(30);
myPriorityQueue.push(10);
myPriorityQueue.push(20);
// Print the size of the priority queue
cout << "Size of priority queue is: "
<< myPriorityQueue.size() << endl;
// Print the elements in the priority queue
cout << "Elements in a priority queue are: ";
while (!myPriorityQueue.empty()) {
// Print the top (maximum) element of the priority
// queue
cout << myPriorityQueue.top() << " ";
// Remove the top element from the priority queue
myPriorityQueue.pop();
}
return 0;
}
Output
Size of priority queue is: 3 Elements in a priority queue are: 30 20 10
Time Complexity: O(n log n)
Auxilliary Space: O(n)
Advantages of Container Adapters
Container adapters provide several benefits:
- Simplified interface for common data structures.
- Built on existing STL containers, reducing implementation effort.
- Safer access by exposing only required operations.
- Flexible underlying container selection in many cases.
Limitations of Container Adapters
Container adapters also have some restrictions:
- Do not support iterators.
- Cannot access arbitrary elements directly.
- Provide only a limited set of operations.
- Less flexible than sequence containers like vector or list.