C++ Standard Template Library (STL)
Last Updated :
17 Mar, 2025
The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorithms and containers.
STL was originally designed by Alexander Stepanov and was later accepted as the part of C++ standard in C++ 98. It is a generalized library so we can use it with almost every data type without repeating the implementation code.
Components of STL
The components of STL are the features provided by Standard Template Library (STL) in C++ that can be classified into 4 types:
- Containers
- Algorithms
- Iterators
- Functors
These components are designed to be efficient, flexible, and reusable, making them an integral part of modern C++ programming.
Containers
Containers are the data structures used to store objects and data according to the requirement. Each container is implemented as a template class that also contains the methods to perform basic operations on it. Every STL container is defined inside its own header file.
Containers can be further classified into 4 types:
- Sequence Containers
- Container Adaptors
- Associative Containers
- Unordered Associated Containers
If you want to dive deep into STL and understand its full potential, our Complete C++ Course offers a complete guide to mastering containers, iterators, and algorithms provided by STL.
Sequence Containers
Sequence containers store the data in the linear manner. They are also used to implement container adaptors.
There are 5 sequence containers in C++ STL:
- Arrays: The STL array is an implementation of a compile time non-resizable array. It contains various method for common array operations.
- Vector: An STL vector can be defined as the dynamic sized array which can be resized automatically when new elements are added or removed.
- Deque: Deque or Double-Ended Queue is sequence containers with the feature of expansion and contraction on both ends. It means we can add and remove the data to and from both ends.
- Lists: List container stores data in non-contiguous memory unlike vectors and only provide sequential access to the stored data. It basically implements the doubly linked list.
- Forward Lists: Forward lists also store the data in a sequential manner like lists, but with the difference that forward list stores the location of only the next elements in the sequence. It implements the singly linked list.
Container Adaptors
The container adapters are the type of STL containers that adapt existing container classes to suit specific needs or requirements.
There are 3 container adaptors in C++ STL:
- Stack: STL Stack follows the Last In First Out (LIFO) principle of element insertion and deletion. Also, these operations are performed only at one end of the stack.
- Queue: STL Queue follows the First In First Out (FIFO) principle, means the element are inserted first are removed first and the elements inserted last are removed at last. It uses deque container by default.
- Priority Queue: STL Priority Queue does not follow any of the FIFO or LIFO principle, but the deletion of elements is done on the basis of its priority. So, the element with the highest (by default) is always removed first. By default, it uses vector as underlying container.
Associative Containers
Associative containers are the type of containers that store the elements in a sorted order based on keys rather than their insertion order.
There are 4 associative containers in C++ STL:
- Sets: STL Set is a type of associative container in which each element has to be unique because the value of the element identifies it. By default, the values are stored in ascending order.
- Maps: STL Maps are associative containers that store elements in the form of a key-value pair. The keys have to be unique and the container is sorted on the basis of the values of the keys.
- Multisets: STL Multiset is similar to the set container except that it can store duplicate values.
- Multimaps: STL Multimap is similar to a map container but allows multiple mapped values to have same keys.
Unordered Associative Containers
Unordered associative containers store the data in no particular order, but they allow the fastest insertion, deletion and search operations among all the container types in STL.
There are 4 unordered associative containers in C++ STL:
- Unordered Set: STL Unordered Set stores the unique keys in the form of hash table. The order is randomized but insertion, deletion and search are fast.
- Unordered Multiset: STL Unordered Multiset works similarly to an unordered set but can store multiple copies of the same key.
- Unordered Map: STL Unordered Map stores the key-value pair in a hash table, where key is hashed to find the storage place.
- Unordered Multimap: STL Unordered Multimap container is similar to unordered map, but it allows multiple values mapped to the same key.
Algorithms
STL algorithms offer a wide range of functions to perform common operations on data (mainly containers). These functions implement the most efficient version of the algorithm for tasks such as sorting, searching, modifying and manipulating data in containers, etc. All STL algorithms are defined inside the <algorithm> and <numeric> header file.
There is no formal classification of STL algorithms, but we can group them into two types based on the type of operations they perform:
Manipulative Algorithms
Manipulative algorithms perform operations that modifies the elements of the given container or rearrange their order.
Some of the common manipulative algorithm includes:
- copy: Copies a specific number of elements from one range to another.
- fill: Assigns a specified value to all elements in a range.
- transform: Applies a function to each element in a range and stores the result in another range.
- replace: Replaces all occurrences of a specific value in a range with a new value.
- swap: Exchanges the values of two variables.
- reverse: Reverses the order of elements in a range.
- rotate: Rotates the elements in a range such that a specific element becomes the first.
- remove: Removes all elements with a specified value from a range but does not reduce the container size.
- unique: Removes consecutive duplicate elements from a range.
Non-Manipulative Algorithms
Non-manipulating algorithms are the type of algorithms provided by the Standard Template Library (STL) that operate on elements in a range without altering their values or the order of the elements.
The below are the few examples of the STL's non-manipulative algorithms:
- max_element: Find the maximum element in the given range.
- min_element : To find the minimum element in the given range.
- accumulate: Finds the sum of the elements of the given range.
- count: Counts the occurrences of given element in the range.
- find: Returns an iterator to the first occurrence of an element in the range.
- is_permutation: Checks if one range is a permutation of another.
- is_sorted: Checks if the elements in a range are sorted in non-decreasing order.
- partial_sum: Computes the cumulative sum of elements in a range.
Iterators
Iterators are the pointer like objects that are used to point to the memory addresses of STL containers. They are one of the most important components that contributes the most in connecting the STL algorithms with the containers. Iterators are defined inside the <iterator> header file.
In C++ STL, iterators are of 5 types:
- Input Iterators: Input Iterators can be used to read values from a sequence once and only move forward.
- Output Iterators: Output Iterators can be used to write values into a sequence once and only move forward.
- Forward Iterators: Forward Iterators combine the features of both input and output iterators.
- Bidirectional Iterators: Bidirectional Iterators support all operations of forward iterators and additionally can move backward.
- Random Access Iterators: Random Access Iterators support all operations of bidirectional iterators and additionally provide efficient random access to elements.
Functors
Functors are objects that can be treated as though they are a function. Functors are most commonly used along with STL algorithms. It overloads the function-call operator ()
and allows us to use an object like a function. There are many predefined functors in C++ STL that are defined inside the <functional> header file.
Functors can be classified into multiple types based on the type of operator they perform:
- Arithmetic Functors
- Relational Functors
- Logical Functors
- Bitwise Functors
Arithmetic Functors
- plus - Returns the sum of two parameters.
- minus - Returns the difference of two parameters.
- multiplies - Returns the product of two parameters.
- divides - Returns the result after dividing two parameters.
- modulus - Returns the remainder after dividing two parameters.
- negate - Returns the negated value of a parameter.
Relational Functors
- equal_to - Returns true if the two parameters are equal.
- not_equal_to - Returns true if the two parameters are not equal.
- greater - Returns true if the first parameter is greater than the second.
- greater_equal - Returns true if the first parameter is greater than or equal to the second.
- less - Returns true if the first parameter is less than the second.
- less_equal - Returns true if the first parameter is less than or equal to the second.
Logical Functors
- logical_and - Returns the result of Logical AND operation of two parameters.
- logical_or - Returns the result of Logical OR operation of two parameters.
- logical_not - Returns the result of Logical NOT operation of the parameters.
Bitwise Functors
- bit_and - Returns the result of Bitwise AND operation of two parameters.
- bit_or - Returns the result of Bitwise OR operation of two parameters.
- bit_xor - Returns the result of Bitwise XOR operation of two parameters.
Utility and Memory Library
The Utility Library is a collection of utility components provided by the Standard Template Library (STL) that does not fall in the above categories. It offers various features such as pairs, tuples, etc.
The memory library contains the function that helps users to efficiently manage the memory such as std::move, smart pointers, etc.
- Pair: Container to store and manipulate heterogeneous data.
- Move Semantics: It allows the transfer of resources from one object to another without copying.
- Smart Pointers: They are a wrapper over the raw pointers and helps in avoiding errors associated with pointers.
- Utility Functions: Utility functions in C++ provide important operations like std::forward to facilitate efficient, generic and safe code manipulation.
- Integer Sequence: Enable compile-time generation of integer sequences, useful in metaprogramming.
Benefits of C++ Standard Template Library (STL)
The key benefit of the STL is that it provides a way to write generic, reusable code and tested code that can be applied to different data types. This means you can write an algorithm once and then use it with other types of data without having to write separate code for each type.
Other benefits include:
- STL provides flexibility through customizable templates, functors, and lambdas.
- Pre-implemented tools let you focus on problem-solving rather than low-level coding.
- STL handles memory management which reduces common errors like memory leaks.
Limitations of C++ Standard Template Library (STL)
The major limitation of the C++ Standard Template Library (STL) is Performance Overheads. While STL is highly optimized for general use cases, its generic nature can lead to less efficient memory usage and execution time compared to custom and specialized solutions.
Other limitations can be:
- Complexity of debugging.
- Lack of control over memory management.
- Limited support for concurrent programming.
- Limited flexibility with custom data structures integrating.
Despite these limitations, STL remains an invaluable part of C++ programming, offering a wide range of powerful and flexible tools.
Similar Reads
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Containers in C++ STL Standard Template Library (STL) provides the built-in implementation of commonly used data structures known as containers. A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the data type
3 min read
STL Containers
STD::array in C++The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
Vector in C++ STLC++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
List in C++ STLIn C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known.Example:C++#include <iostream
7 min read
Forward List in C++ STLIn C++, forward_list container provides the implementation of singly linked list data structure. It stores data in non-contiguous memory where each element points to the next element in the sequence. This makes insertion and deletion faster once the position of the element is known.Example:C++#inclu
7 min read
Stack in C++ STLIn C++, stack container follows LIFO (Last In First Out) order of insertion and deletion. It means that most recently inserted element is removed first and the first inserted element will be removed last. This is done by inserting and deleting elements at only one end of the stack which is generally
5 min read
Queue in C++ STLIn C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat
4 min read
Deque in C++ STLIn C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using
6 min read
Set in C++ STLIn C++, sets are associative container which stores unique elements in some sorted order. By default, it is sorted ascending order of the keys, but this can be changed as per requirement. It provides fast insertion, deletion and search operations.Example: C++#include <iostream> #include <se
7 min read
Map in C++ STLIn C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read
Multiset in C++ STLIn C++, multiset is an associative container similar to the set, but it can store multiple elements with same value. It is sorted in increasing order by default, but it can be changed to any desired order. It provides fast insertion, deletion and search operations.Example:C++#include <iostream
6 min read
Multimap in C++ STLIn C++, multimap is an associative container similar to map, but it can have multiple elements with same keys. It stores all the elements in increasing order based on their keys by default but can be changed if required. It provides fast insertion, deletion and search on this sorted data.Example:CPP
8 min read
Unordered Sets in C++ STLIn C++, unordered_set is an unordered associative container that stores unique elements. Unlike set, it stores its elements using hashing. This provides average constant-time O(1) search, insert, and delete operations but the elements are not sorted in any particular order.Example:C++#include <io
6 min read
Unordered Map in C++ STLIn C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not
7 min read
Unordered Multiset in C++ STLIn C++, unordered multiset is an unordered associative container that works similarly to an unordered set, but it can store multiple copies of the same value. It provides fast insert, delete and search operations using hashing, but the elements are not in any particular order.Example: C++#include
7 min read
Unordered Multimap in C++ STLIn C++, the unordered_multimap is an unordered associative container that stores data in the form of key-value pairs. It is similar to unordered map, but it allows multiple elements with the same key. It provides fast insertion, deletion and search operations in O(1) time by using hashing.Example:C+
7 min read
Introduction to Iterators in C++ An iterator is an object like a pointer that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location usi
4 min read
STL Iterators
Forward Iterators in C++After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main t
6 min read
Output Iterators in C++After going through the template definition of various STL algorithms like std::copy, std::move, std::transform, you must have found their template definition consisting of objects of type Output Iterator. So what are they and why are they used ?Output iterators are one of the five main types of ite
6 min read
Forward Iterators in C++After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main t
6 min read
Bidirectional Iterators in C++After going through the template definition of various STL algorithms like std::reverse, std::next_permutation and std::reverse_copy you must have found their template definition consisting of objects of type Bidirectional Iterator. So what are they and why are they used ? Bidirectional iterators ar
7 min read
Random Access Iterators in C++After going through the template definition of various STL algorithms like std::nth_element, std::sort, you must have found their template definition consisting of objects of type Random-access Iterator. So what are they and why are they used?Random-access iterators are one of the five main types of
6 min read
Iterators in C++ STLAn iterator in C++ is a pointer-like object that points to an element of the STL container. They are generally used to loop through the contents of the STL container in C++. The main advantage of STL iterators is that they make the STL algorithms independent of the type of container used. We can jus
10 min read
C++ STL Algorithm Library Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
STL Algorithms
sort() in C++ STLIn C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backward()Various varieties of copy() exist in C++ STL that allows to perform the copy operations in different manners, all of them having their own use. These all are defined in header <algorithm>. This article introduces everyone to these functions for usage in day-to-day programming. 1. copy(strt_ite
5 min read
max_element in C++ STLThe std::max_element() in C++ is an STL algorithm that is used to find the maximum element in the given range. It is defined inside the <algorithm> header file. In this article, we will learn how to find the maximum element in the range using std::max_element() in C++.Example:C++// C++ program
4 min read
find() in C++ STLC++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++.C++#include <bits/stdc++.h> using nam
2 min read
for_each loop in C++Apart from the generic looping techniques, such as "for, while and do-while", C++ in its language also allows us to use another functionality which solves the same purpose termed "for-each" loops. This loop accepts a function which executes over each of the container elements. This loop is defined i
5 min read
Algorithm Library Functions in C++ STLNon-modifying sequence operations std :: all_of : Test condition on all elements in rangestd :: any_of : Test if any element in range fulfills conditionstd :: none_of : Test if no elements fulfill conditionstd :: for_each : Apply function to rangestd :: find : Find value in rangestd :: find_if : Fin
4 min read
Functors in C++ Please note that the title is Functors (Not Functions)!! Consider a function that takes only one argument. However, while calling this function we have a lot more information that we would like to pass to this function, but we cannot as it accepts only one parameter. What can be done? One obvious an
3 min read
C++ STL Cheat Sheet The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and
15+ min read
Top C++ STL Interview Questions and Answers The Standard Template Library (STL) is a set of C++ template classes that are used to implement widely popular algorithms and data structures such as vectors, lists, stacks, and queues. It is part of the C++ Language ISO standard. STL is a popular topic among interviewers, so it is useful for both f
15+ min read