Table of Contents
In this tutorial, you will learn about C++ STL containers with the help of examples.
What is Container in C++ ?
A container is an object that stores a collection of objects of a specific type. They are implemented as class templates, which allows a great flexibility in the types supported as elements. For example, if we need to store a list of names of cities, we can use a vector
.
C++ STL provides different types of containers based on our requirements.
Types of STL Container in C++
In C++, there are generally 3 kinds of STL containers are as follows
- Sequential Containers
- Associative Containers
- Unordered Associative Containers
1. Sequential Containers
In C++, sequential containers allow us to store elements that can be accessed in sequential order.
Internally, sequential containers are implemented as arrays or linked lists data structures.
Types of Sequential Containers
- Array
- Vector
- Deque
- List
- Forward List
Let’s understand how to use the vector class as Sequential Container.
#include<iostream>
#include<vector>
using namespace std;
int main()
// initialize a vector of int type
vector<int> numbers = {1,100,10,70,100};
// print the vector
cout<<"Numbers are:";
// ranged for loop
for(auto &num:numbers){
cout << num << ", ";
}
return 0;
}
Output
Numbers are: 1, 100, 10, 70, 100,
In above example, we have used a ranged for loop to print each element of the container.
2. Associative Containers
In C++, associative containers allow us to store elements in sorted order. The order doesn’t depend upon when the element is inserted.
Internally, they are implemented as binary tree data structures.
Types of Associative Containers
- Set
- Map
- Multiset
- Multimap
Let’s understand how to use the set class as Associative Containers.
#include<iostream>
#include<set>
using namespace std;
int main() {
// initialize a set of int type
set<int> numbers = {1, 100, 10, 70, 100};
// print the set
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
Output
Numbers are: 1, 10, 70, 100,
In the above example, we have created an associative container using the set
class and, you can see that the output numbers are sorted in ascending order with duplicate number removed.
we have used a ranged for loop to print each element of the container as used in above example.
3. Unordered Associative Containers
In C++, STL Unordered Associative Containers provide the unsorted versions of the associative container.
Internally, unordered associative containers are implemented as hash table data structures.
Types of Unordered Associative Containers
- Unordered Set
- Unordered Map
- Unordered Multiset
- Unordered Multimap
Let’s understand how to use the unordered set class as Unordered Associative Containers.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
// initialize an unordered_set of int type
unordered_set<int> numbers = {1, 100, 10, 70, 100};
// print the set
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
Output
Numbers are: 70, 10, 100, 1,
In the above example, we have created an associative container using the unordered_set
class and, you can see that the output numbers are not sorted in ascending order with duplicate number removed.
NOTE : Associative containers allow us to store elements in sorted order whereas in unordered associative containers not allow us to store elements in sorted order but in both with duplicate elements are removed.
What is the difference between associative containers and unordered?
The underlying difference is that associative containers are based on tree structures, whereas unordered associative containers are based on another form of data structure called a hash table.