Containers are class templates in the Standard Template Library (STL) that store and manage collections of elements efficiently.
- Provide ready-to-use implementations of common data structures.
- Support efficient storage, retrieval, insertion, and deletion of elements.
- Work with different data types through templates.
Types of Containers
STL containers are broadly classified into four categories based on how they store and organize elements.
1. Sequence Containers
Sequence containers implement linear data structures in which the elements can be accessed sequentially. Following are the sequence containers in C++ STL:
- Array Fixed-size sequence container with contiguous storage.
- Vector Resizable array supporting efficient random access.
- Deque Double-ended sequence container supporting efficient insertion and deletion at both ends.
- List Doubly linked list supporting efficient insertion and deletion at known positions.
- Forward List Singly linked list supporting forward traversal
2. Associative Containers
Associative containers maintain elements in sorted order based on keys or values. Searching, insertion, and deletion generally have O(log n) complexity.
- Set Stores unique elements in sorted order.
- Map Stores key-value pairs with unique, sorted keys.
- Multiset Stores sorted elements and allows duplicate values.
- Multimap Stores sorted key-value pairs and allows duplicate keys.
3. Unordered Associative Containers
Unordered associative containers implement unsorted hashed data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity).
- Unordered set stores unique elements using hashing.
- Unordered map stores key-value pairs with unique keys.
- Unordered multiset stores elements using hashing and allows duplicates.
- Unordered multimap stores key-value pairs and allows duplicate keys.
4. Container Adapters
Container adapters provide a different interface for other containers. They adapt the behavior of underlying containers to fit specific use cases.
- Stack follows Last-In, First-Out (LIFO) order.
- Queue follows First-In, First-Out (FIFO) order.
- Priority queue: provides access to the highest-priority element.
Common Operations
Most STL containers support a common set of operations for managing and accessing elements.
| Operation | Description |
|---|---|
| insert() | Adds an element to the container |
| erase() | Removes an element from the container |
| size() | Returns the number of elements in the container |
| empty() | Checks whether the container is empty |
| clear() | Removes all elements from the container |
| begin() | Returns an iterator to the first element |
| end() | Returns an iterator to the position after the last element |
#include <iostream>
#include <vector>
#include <map>
#include <stack>
using namespace std;
int main() {
// Sequence container
vector<int> nums = {10, 20, 30};
nums.push_back(40);
cout << "Vector: ";
for (int n : nums) {
cout << n << " ";
}
cout << endl;
// Associative container
map<int, string> students;
students[1] = "Amit";
students[2] = "Riya";
cout << "Student: " << students[1] << endl;
// Container adaptor
stack<int> s;
s.push(5);
s.push(10);
cout << "Stack Top: " << s.top();
return 0;
}
Output
Vector: 10 20 30 40 Student: Amit Stack Top: 10
Explanation:
- vector stores integers and adds an element using push_back().
- map stores student names using unique integer keys.
- stack provides access to the most recently inserted element using top().
Advantages of STL Containers
- Provide reusable implementations of common data structures.
- Offer documented complexity guarantees for supported operations.
- Support different element types that meet the container's requirements.
- Reduce the need to implement data structures manually..

