C++ STL (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose classes and functions with templates that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.
It is a library of container classes, algorithms, and iterators.
STL has four components
- Containers
- Iterators
- Algorithms
- Functions
Containers
Containers are used to manage collections of objects of a certain kind. There are several different types of containers like deque, list, vector, map etc.
- Sequence Containers: implement data structures which can be accessed in a sequential manner.
- Container Adaptors : provide a different interface for sequential containers.
- Associative Containers : implement sorted data structures that can be quickly searched (O(log n) complexity).
- Unordered Associative Containers : implement unordered data structures that can be quickly searched
Container class templates
Sequence containers:
Name | Description |
---|---|
array | Array class (class template ) |
vector | Vector (class template ) |
deque | Double ended queue (class template ) |
forward_list | Forward list (class template ) |
list | List (class template ) |
Container adaptors:
Name | Description |
---|---|
stack | LIFO stack (class template ) |
queue | FIFO queue (class template ) |
priority_queue | Priority queue (class template ) |
Associative containers:
Name | Description |
---|---|
set | Set (class template ) |
multiset | Multiple-key set (class template ) |
map | Map (class template ) |
multimap | Multiple-key map (class template ) |
list | List (class template ) |
Unordered associative containers:
Name | Description |
---|---|
unordered_set | Unordered Set (class template ) |
unordered_multiset | Unordered Multiset (class template ) |
unordered_map | Unordered Map (class template ) |
unordered_multimap | Unordered Multimap (class template ) |
Iterators
Iterators are used for working on a sequence of values. Iterators are used to point at the memory addresses of STL containers. They are the major feature that allows generality in STL.
Operations of iterators :-
begin(), end(). next(). prev() etc.
Algorithm
In STL, different types of algorithms can be implemented with the help of iterators. Algorithms can be defined as functions applied to the containers and provide operation for the content of the container.
Types of algorithm :
- Sorting algorithm
- Searching algorithm
- Non-Manipulating Algorithms : sort(), reverse(), max_element(), min_element(), lower_bound(), upper_bound() etc.
- Manipulating Algorithms : arr.erase(), next_permutation(), prev_permutation() etc
- Algorithm on array : all_of(), any_of(), none_of(), copy_n() and iota() etc
- Partition algorithm : partition(), is_partitioned(), stable_partition() etc
- Numeric algorithm : min(), max(), swap(), shift() etc
Function objects
A function object is also known as a functor; it is an object of a class that provides a definition for the operator(). Suppose you have declared an object of some class, then you can use that object just like the function. For example :
Advantages of STL:
- Reusability: STL provides a way to write generic, reusable code that can be applied to different data types. This can lead to more efficient and maintainable code.
- Efficient algorithms: Many of the algorithms and data structures in the STL are implemented using optimised algorithms, which can result in faster execution times compared to custom code.
- Improved code readability: The STL provides a consistent and well-documented way of working with data, which can make your code easier to understand and maintain.
Disadvantages of STL:
- Learning curve: The STL can be difficult to learn, especially for beginners, due to its complex syntax and use of advanced features like iterators and function objects.
- Lack of control: When using the STL, you have to rely on the implementation provided by the library, which can limit your control over certain aspects of your code.