Standard Template Library
#
IntroductionThe power of the Standard Template Library is that it provides generic containers and generic algorithms in such a way that most of the algorithms work on most of the containers, no matter what type of data the containers store. Performance is a very important aspect of the Standard Library. The goal is to make the Standard Library containers and algorithms as fast as, or faster than hand-written code
note
The use of any functionality provided by C headers is discouraged in favor of true C++ functionality
The Standard Library makes heavy use of the C++ features called templates and operator overloading. A C++ programmer who wishes to claim language expertise is expected to be familiar with the Standard Library. You can save yourself immeasurable time and energy by incorporating Standard Library containers and algorithms into your programs instead of writing and debugging your own versions. Now is the time to master this Standard Library.
STL has Four components
#
ContainersContainers are the heart of the STL. The library defines four categories of containers, each for a different purpose:
- sequence containers
- associate containers
- container adapters
- pseudo-containers.
Each container category has been defined for a group of applications. Each container in each category is used for a specific purpose.
#
AlgorithmsAlgorithms are operations that we apply to the container elements. They are divided into four categories:
- non-mutating algorithms : which do not change the container structure
- mutating algorithms : which change the structure
- sorting algorithms : which reorder elements in a container
- numeric algorithms : which apply mathematical operations to the elements.
#
IteratorsAn iterator allows us to access each element individually and apply the desired operation to it. This means that we do not need one algorithm that counts one type and another that counts another type. An algorithm can be applied to any container that provides the type of iterators that the container supports.
#
Functions and Function ObjectsTo apply algorithms to the container, STL uses functions or function objects in the algorithm definition. They allow the STL to define a generic algorithm and use functions or function objects to make the algorithm specific. In the first case, we need a function definition; in the second case, we need a class for which the operator() is defined. The user defines a function for the first case, but the class is normally defined in the STL library and the user can only call the constructor of the class.
#
IteratorsAn iterator is an abstraction of a pointer. It is a class type that has a pointer as a data member and predefined operations that can be applied to the pointer member. We can instantiate an object of an iterator and then apply the operations defined for it.
One benefit of an iterator over a pointer is that we cannot limit the operations defined for a pointer, nor can we augment the operations defined for a pointer. But we can do both for an iterator.
Another advantage of an iterator is that it can hide the internal structure of a container. Each container can define its own iterator type whose design is hidden from the user, but the user can create an iterator of that type and access the objects in the container. The way iterators work with most containers is that the STL defines several internal iterators that are fixed and cannot move.
We can categorize iterators into five types
- input iterator
- output iterator
- forward iterator
- bidirectional iterator
- random-access iterator
tip
Only the sequential containers, ordered associative containers, and unordered associative containers provide iterators. The container adaptors and bitset class do not support iteration over their elements.
Container | Type Of Iterator Supported |
---|---|
Vector | Random-access |
Deque | Random-access |
List | Bidirectional |
Map | Bidirectional |
MultiMap | Bidirectional |
Set | Bidirectional |
MultiSet | Bidirectional |
Stack | โ No Iterator Supported |
Queue | โ No Iterator Supported |
Priority-Queue | โ No Iterator Supported |
#
Input IteratorAn input iterator can use the dereference operator only to read from a container; it is not allowed to write to it. In other words, an input iterator treats the container as a source of data items to read.
#
Output IteratorAn output iterator can use the dereference operator to only write to a container; it is not allowed to read from it.
#
Forward IteratorA forward iterator can read or write elements. Its functionality is the combination of the input and output iterators.
#
Bidirectional IteratorA bidirectional iterator can move in both directions: backward and forward. The ++
and โโ
operators are defined for this iterator.
#
Random-Access IteratorA random-access iterator has the capabilities of a bidirectional iterator, and in addition it supports the add (+
) operator and the subtract operator (โ
). It also provides four relational operators (<
, <=
, >
, and >=
) that are not provided by the other iterators. These operators allow us to use the index operator [ ], which requires the +, โ, and relational operators for forward or backward movement.
#
CheetsheetIterator | read | write | * | ++ | โ- | == , != | < , <= , > , >= | + , โ |
---|---|---|---|---|---|---|---|---|
input | โ | โ | โ | โ | ||||
output | โ | โ | โ | โ | ||||
forward | โ | โ | โ | โ | โ | |||
bidirectional | โ | โ | โ | โ | โ | โ | ||
random-access | โ | โ | โ | โ | โ | โ | โ | โ |
#
ConstantnessA container can also define two types of iterators
#
Type: const iteratorThe type const iterator
defines an iterator type that is a constant object. In other words, it cannot be changed after it is created. We cannot move the const iterator to point to another element. It is like the name of an array that is a constant pointer.
#
Type: const_iteratorThe type const_iterator
defines an iterator type in which the dereferenced item is an rvalue
(cannot be changed by the iterator). This is similar to when we declare an array whose elements are constants.
info
const_iterators
and const_reverse_iterators
provide read-only access to elements of the container.
#
Containersnote
The C++ Standard Library containers are homogeneous: they allow elements of only one type in each container.
#
Sequence ContainersSequence containers are ordered collections in which every element has a certain position. This position depends on the time and place of the insertion, but it is independent of the value of the element.
#
General public interface of Sequence ContainersThe abbreviation SC must be replaced with one of the three sequence containers (vector, deque, or list) when the corresponding column is ticked. Each of these containers is a template class in which T defines the type of the element in the container. The typename T can be any built-in or user-defined type. We have used iter for an iterator, inIter for an input iterator, and pred for a Boolean function returning true or false.
Constructors, assignment, and destructor | Vector | Deque | List |
---|---|---|---|
SC <T> :: SC() | โ | โ | โ |
SC <T> :: SC(size\*type n, const T& value = T()) | โ | โ | โ |
SC <T> :: SC(const_iter first, const_iter last) | โ | โ | โ |
SC <T> :: SC(const SC <T>& other) | โ | โ | โ |
SC <T>& SC <T> :: operator =(const SC <T> & other) | โ | โ | โ |
SC <T> :: ~SC() | โ | โ | โ |
Size and capacity | Vector | Deque | List |
---|---|---|---|
size_type SC <T> :: size() | โ | โ | โ |
size_type SC <T> :: max_size() | โ | โ | โ |
void SC <T> :: resize(size_type n, T value = T() ) | โ | โ | โ |
bool SC <T> :: empty() | โ | โ | โ |
size_type SC <T> :: capacity() | โ | ||
void SC <T> :: reserve(size_type, n) | โ |
Accessing elements (constant and nonconstant versions) | Vector | Deque | List |
---|---|---|---|
T& SC <T> :: front() | โ | โ | โ |
T& SC <T> :: back() | โ | โ | โ |
T& SC <T> :: operator[ ]()(size_type index) | โ | โ | |
T& SC <T> :: at(size_type index) | โ | โ |
Iterators (regular and constant* iterator versions) | Vector | Deque | List |
---|---|---|---|
iter SC <T> :: begin() | โ | โ | โ |
iter SC <T> :: end() | โ | โ | โ |
reverse_iter SC <T> :: rbegin() | โ | โ | โ |
reverse_iter SC <T> :: rend() | โ | โ | โ |
Insertion | Vector | Deque | List |
---|---|---|---|
void SC <T> :: push_front(const T& value) | โ | โ | |
void SC <T> :: push_back(const T& value) | โ | โ | โ |
iter SC <T> :: insert(iter pos, const T& value) | โ | โ | โ |
void SC <T> :: insert(iter pos, size_type n, const T& value) | โ | โ | โ |
void SC <T> :: insert(iter pos, InIter first, inIter last) | โ | โ | โ |
Erasure | Vector | Deque | List |
---|---|---|---|
void SC <T> :: pop_front() | โ | โ | |
void SC <T> :: pop_back() | โ | โ | โ |
iter SC <T> :: erase(iter pos) | โ | โ | โ |
iter SC <T> :: erase(iter first, iter second) | โ | โ | โ |
void SC <T> :: clear() | โ | โ | โ |
void SC <T> :: remove(const T& value) | โ | ||
void SC <T> :: remove_if(pred p) | โ | ||
void SC <T> :: unique(pred p) | โ |
Splice, merge, and sort | Vector | Deque | List |
---|---|---|---|
void SC <T>::splice(iter pos, SC<T> other) | โ | ||
void SC <T>::splice(iter pos, SC<T> other, iter other) | โ | ||
void SC <T>::splice(iter pos, SC<T> other, iter i, iter j) | โ | ||
void SC <T>:: merge(SC<T> other) | โ | ||
void SC <T> :: sort() | โ |
Swapping | Vector | Deque | List |
---|---|---|---|
void SC <T> :: swap(SC<T>& other) | โ | โ | โ |
Global functions (op means <, <=, >, >=, == or !=) | Vector | Deque | List |
---|---|---|---|
bool operator op(const SC<T> left, const SC<T> right) | โ | โ | โ |
void swap(SC<T>& left, SC<T>& right) | โ | โ | โ |
#
Container AdaptorsThe term container adaptor, refers to an "adaptation" of one of the first-class containers by modifying and restricting its interface for some special purpose. The container adapters defined in the library are stack, queue, and priority_queue.
note
Container adapters cannot be used with algorithms because they do not provide support for iterators.
#
Public InterfaceThe abbreviation Ad
can be replaced with one of the three adapters (stack, queue, or priority_queue) when the corresponding column is ticked. Each of these adapters is a template class in which T defines the type of the element in the container.
Constructor | Stack | Queue | Priority_Queue |
---|---|---|---|
Ad <T> :: Ad () | โ | โ | โ |
Checking size and emptiness | Stack | Queue | Priority_Queue |
---|---|---|---|
size_type Ad<T> :: size() const | โ | โ | โ |
bool Ad <T> :: empty() const | โ | โ | โ |
Accessing elements | Stack | Queue | Priority_Queue |
---|---|---|---|
T& Ad <T> :: front() | โ | ||
T& Ad <T> :: back() | โ | ||
T& Ad <T> :: top() | โ | โ |
Insertion | Stack | Queue | Priority_Queue |
---|---|---|---|
void Ad <T> :: push(const T& elem) | โ | โ | โ |
Erasure | Stack | Queue | Priority_Queue |
---|---|---|---|
void Ad <T> :: pop() | โ | โ | โ |