In C++, STL (Standard Template Library), containers are objects used to store the other objects’ collections. They work similarly to class templates and support a large number of element types and provide member functions to access their elements directly or via iterators.
Types of C++ STL Containers
In C++, there are three types of STL containers, which are listed below:
1: Sequential Containers
In C++, sequential containers enable us to store items that can be retrieved sequentially. These containers are encoded as arrays or linked lists of data structures. Some types of sequential containers are given below.
- Vector: It is a dynamically sized array that is stored in memory in a contiguous manner.
- Deque: It represents a double-ended queue that supports operations for both insertion and deletion.
- Array: It is a static array allocated during compilation while keeping its size fixed.
- List: It is a doubly-linked list that performs fast insertion and deletion of elements at any place in the list.
- Forward List: It is a singly-linked list like a list but you can only traverse it in one direction.
Example
In this example, we will use the vector class to show how a sequential container operates.
#include <vector>
using namespace std;
int main() {
// initialize a vector of int type
vector<int> nums = {10, 2, 16, 70, 5};
// print the vector
cout << "Numbers are: ";
for(auto &i: nums)
{
cout << i << ", ";
}
return 0;
}
The above code above demonstrates the use of sequential containers in vector format, which allows for the storage of integer arrays. The program initializes a vector of type integer, assigns values ​​to it, and prints them using a loop. This example shows how it’s easy for storing and accessing data in C++ using the sequential container.
2: Associative Containers
The associative containers allow us to store elements in the particular order defined by the comparison operator. Unlike sequential containers, the order of elements in associative containers is maintained using keys that allow users to organize and access the elements. When an element is inserted into an associative container, it is automatically sorted at the correct position based on its key. These types of containers are implemented internally like binary tree data structures.
The associative containers are categorized as:
- Map: a collection of key-value pairs that have been sorted using unique keys
- Multimap: a collection of key-value pairs that have been sorted using keys
- Set: Unique keys collected and arranged by keys.
- Multiset: a collection of keys that have been sorted using keys
Example
To illustrate how an associative container works, we will use the set class in this example.
#include <set>
using namespace std;
int main()
{
// initialize a set of int type
set<int> nums = {10, 2, 16, 70, 5};
// print the set
cout << "Numbers are: ";
for(auto &i: nums)
{
cout << i << ", ";
}
return 0;
}
The above code initializes a set of integers in C++, which is an example of an associative container. The set ensures that the elements are sorted in ascending order by default. The code then prints out the numbers in the set using a for loop.
3: Unordered Associative Containers
In C++, unordered associative containers are used for providing the unsorted versions of an associative container. They are internally implemented like hash table data structures. The associative containers are categorized as:
- Unordered Map: a collection of key-value pairs that have been hashed using unique keys.
- Unordered Multimap: the key-value pairs collection that has been hashed using keys.
- Unordered Set: a collection of unique keys that have been hashed using keys.
- Unordered Multiset: a collection of keys that have been hashed using keys.
Example
In order to illustrate how an unordered associative container works, we will use the unordered set class in this example.
#include <unordered_set>
using namespace std;
int main()
{
// initialize an unordered_set of int type
unordered_set<int> nums = {10, 2, 16, 70, 5};
// print the set
cout << "Numbers are: ";
for(auto &i: nums)
{
cout << i << ", ";
}
return 0;
}
Conclusion
An STL C++ container is the holder object to store the collection of other objects. They work similarly to class templates and support a large number of element types. In this tutorial, we discussed the most commonly used types of STL C++ containers, which are sequential containers, associative containers as well as unordered associative containers.