The Standard Template Library (STL) is a powerful component of the C++ Standard Library that provides a collection of generic classes and functions. It simplifies programming by offering reusable components, which can be used to implement common data structures and algorithms efficiently. Understanding STL is crucial for any serious C++ programmer as it can significantly reduce development time and improve code quality.
In this tutorial, we will explore the core components of STL, particularly focusing on containers. We'll discuss different types of containers, their characteristics, and when to use each one. By the end of this tutorial, you should have a solid understanding of how to leverage STL containers in your C++ programs.
The Standard Template Library (STL) is a set of templates classes and functions that implement common data structures and algorithms. It provides a high-level abstraction for programming, allowing developers to write more generic and reusable code.
STL is divided into three main components:
STL provides several types of containers, each suited for different use cases:
Sequence Containers:
Associative Containers:
Unordered Associative Containers:
Choosing the right container is crucial for optimizing your program. Here are some guidelines:
std::vector when you need a dynamic array with fast random access and moderate insertion/deletion at the end.std::list when frequent insertions and deletions are required, especially at positions other than the end.std::deque when you need efficient insertion and deletion from both ends.std::set or std::map when you need a collection of unique elements that are automatically sorted.std::unordered_set or std::unordered_map when you need fast average-time complexity for insertions and lookups, with no guarantee of order.Let's create a simple program that demonstrates the use of different containers. This program will store a list of integers in each container type and print them out.
1#include <iostream>2#include <vector>3#include <list>4#include <deque>5#include <set>6#include <map>7#include <unordered_set>8#include <unordered_map>910int main() {11// Vector example12std::vector<int> vec = {1, 2, 3, 4, 5};13std::cout << "Vector: ";14for (const auto& elem : vec) {15std::cout << elem << " ";16}17std::cout << std::endl;1819// List example20std::list<int> lst = {1, 2, 3, 4, 5};21std::cout << "List: ";22for (const auto& elem : lst) {23std::cout << elem << " ";24}25std::cout << std::endl;2627// Deque example28std::deque<int> deq = {1, 2, 3, 4, 5};29std::cout << "Deque: ";30for (const auto& elem : deq) {31std::cout << elem << " ";32}33std::cout << std::endl;3435// Set example36std::set<int> st = {1, 2, 3, 4, 5};37std::cout << "Set: ";38for (const auto& elem : st) {39std::cout << elem << " ";40}41std::cout << std::endl;4243// Map example44std::map<int, std::string> mp;45mp[1] = "One";46mp[2] = "Two";47mp[3] = "Three";48mp[4] = "Four";49mp[5] = "Five";50std::cout << "Map: ";51for (const auto& elem : mp) {52std::cout << elem.first << ":" << elem.second << " ";53}54std::cout << std::endl;5556// Unordered Set example57std::unordered_set<int> ust = {1, 2, 3, 4, 5};58std::cout << "Unordered Set: ";59for (const auto& elem : ust) {60std::cout << elem << " ";61}62std::cout << std::endl;6364// Unordered Map example65std::unordered_map<int, std::string> ump;66ump[1] = "One";67ump[2] = "Two";68ump[3] = "Three";69ump[4] = "Four";70ump[5] = "Five";71std::cout << "Unordered Map: ";72for (const auto& elem : ump) {73std::cout << elem.first << ":" << elem.second << " ";74}75std::cout << std::endl;7677return 0;78}
Vector: 1 2 3 4 5 List: 1 2 3 4 5 Deque: 1 2 3 4 5 Set: 1 2 3 4 5 Map: 1:One 2:Two 3:Three 4:Four 5:Five Unordered Set: 5 4 3 2 1 Unordered Map: 5:Five 4:Four 3:Three 2:Two 1:One
std::set, but each element is a key-value pair. The keys are unique and sorted.std::unordered_set, but each element is a key-value pair. The keys are unique.| Container Type | Characteristics |
|---|---|
std::vector | Dynamic array with fast random access, moderate insertion/deletion at the end |
std::list | Doubly linked list for efficient insertion and deletion at any position |
std::deque | Double-ended queue for fast insertion and deletion from both ends |
std::set | Sorted set of unique elements |
std::map | Sorted key-value pairs where keys are unique |
std::unordered_set | Unsorted set of unique elements with fast average-time complexity |
std::unordered_map | Unsorted key-value pairs where keys are unique, with fast average-time complexity |
In the next tutorial, we will delve deeper into one of the sequence containers: std::array. We'll explore its features, use cases, and how it compares to other containers. Stay tuned!