Container Adaptors in C++

Container Adaptors are the third type of Containers in C++. Just like Sequence and Associative Containers, there goal is to store objects in a particular manner with certain functionality.

Container Adaptors take your regular containers (such as Sequence Containers) and limit functionality in such a way to create a new container type with new functionality. To better help you visualize this, let’s use a short example.

Imagine a container that allows you to enter and remove elements from both ends. Now remove it’s ability to add and remove elements from the end, and now we have a new type of container!


Stack

The Stack is a very important data structure, widely used in all of programming. It follows the concept of L.I.F.O (Last-in, First out). Another way of saying this would be, First in, Last out. Any new element added to the stack is placed “on top” of the stack.

If you need help visualizing this, imagine yourself stacking a bunch of objects on top of each other, and then trying to access any particular object in the stack. You will find that you can access any object till all the objects stacked on top of it are removed. This is the concept of the Stack in C++.

#include <iostream>
#include <stack>
using namespace std;


int main() {
    stack<int> s;

    s.push(4);
    s.push(1);
    s.push(3);

    while (!s.empty()) {
        cout << s.top() << endl;
        s.pop();
    }
}
3
1
4

Stack Functions:

empty(): Returns whether the stack is empty
size(): Returns the current size of the stack
top(): Returns a reference to the top most element of the stack
push(element): Adds an element to the top of the stack  
pop(): Deletes the top most element in the stack
swap(): Swaps the contents of two Stacks’.
emplace(): Creates a new element, then pushes it to the back of the Stack.


Queue

The Queue is another important C++ Container Adaptor like the stack, but with a fundamental difference. Queue’s follow the concept of F.I.F.O, also known as First In, First out. If 5 elements are added in the Queue, they will be taken out in the same order they were inserted (with the first element being the first, and last element being the last to be removed).

#include <iostream>
#include <queue>
using namespace std;


int main() {
    queue<int> q;

    q.push(4);
    q.push(1);
    q.push(3);

    while (!q.empty()) {
        cout << q.front() << endl;
        q.pop();
    }
}
4
1
3

Queue Functions:

empty(): Returns whether the queue is empty.
size(): Returns the current size of the queue.
swap(): Swaps the contents of two Queue’s.
pop(): Removes the last element in the Queue.
push(elem): Pushes an element to the back of the Queue.
emplace(): Creates a new element, then pushes it to the back of the Queue.
front(): Returns a reference to the first element in the Queue.
back(): Returns a reference to the last element in the Queue.


Priority Queue

The Priority Queue is a variant of the Queue Data Structure. It’s just like a regular Queue but has the concept of priority, which means those elements with the highest priority will be removed from the Queue first. In short, priority queue can be thought of sorted queue’s.

In the case of Integers, the largest Integer will be accessed first. This is shown in the code below.

#include <iostream>
#include <queue>
using namespace std;


int main() {
    priority_queue<int> qp;


    qp.push(4);
    qp.push(1);
    qp.push(3);

    while (!qp.empty()) {
        cout << qp.top() << endl;
        qp.pop();
    }
}
4
3
1

Priority Queue Functions:

empty(): Returns whether the queue is empty.
size(): Returns the current size of the queue.
swap(): Swaps the contents of two Queue’s.
top(): Returns a reference to the top most element of the stack
pop(): Removes the last element in the Queue.
push(elem): Pushes an element to the back of the Queue.
emplace(): Creates a new element, then pushes it to the back of the Queue.


This marks the end of the Container Adaptors Tutorial in C++. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments