The C++ Standard Template Library1
The C++ Standard Template Library (STL) is a collection of templates for several containers (vector, pair, stack, queue, set, etc) that there is no need to re-implement. In this page we will give a short description of some of the templates and methods. For more information see cppreference.com.
To use the STL containers and methods when compiling with `g++
it is necessary to use the flag -std=c++11
.
The rationale behind the STL is to have an interface as uniform as possible. For instance, it is possible to give initialization lists to STL containers.
In the same spirit of uniformity, there are several common methods. For instance the followings.
Method | Cost | |
---|---|---|
bool empty() const |
says whether the container is empty | \Theta(1) |
unsigned int size() const |
returns the size of the container | \Theta(1) |
Moreover, since the C++11 standard, if the compiler can infer the type of a variable, instead of writing explicitly its type, it is possible to use auto
.
Also for
loops got an alternative (less error prone) syntax when used in collections such as set<T>
.
pair<T1,T2>
pair<T1,T2>
is equivalent to
struct pair {
;
T1 first;
T2 second};
A pair
allows to form a pair with the first value of type T1
and the second value of type T2
. It is possible to access the values with first
and second
.
The pair
s are useful since some functions of the STL need to return 2 values. Moreover they have automatically defined some operators, for instance:
- equality
==
(component-wise) - comparison
<
(lexicographically)2
priority_queue<T>
A priority_queue<T>
is a priority queue of elements of T
. By default, the largest element is the one on top, i.e. the first to come out.
Method | Cost | |
---|---|---|
void push(const T& x) |
adds x to the queue |
\Theta(\log n) |
void pop() |
removes the largest element | \Theta(\log n) |
const T& top() const |
returns the top of the queue | \Theta(1) |
To use priority_queue<T>
it is necessary #include <queue>
.
set<T>
A set<T>
is a ordered set of elements of type T
. By default, the elements are ordered increasingly.3
To use set<T>
it is necessary #include <set>
.
An interator set<T>::iterator it
is moved forward with ++it
and backward --it
. To access the element pointed by an iterator it
we use *it
.
Method | Cost | |
---|---|---|
pair<set<T>::iterator,bool> insert (const T& x); |
adds x to the set.If x was not in the set it returns an iterator to the place of x and true . Otherwise, an iterator to x in the set and false |
\Theta(\log n) |
set<T>::iterator begin() |
returns an iterator to the first element of the set | \Theta(1) |
set<T>::iterator end() |
returns an iterator after the last element of the set | \Theta(1) |
set<T>::iterator find(const T& x) const |
looks for x in the set.If x was in the set it returns an iterator pointing to it. Otherwise it returns end() |
\Theta(\log n) |
void erase(iterator it) |
deletes the element pointed by it |
\Theta(1) (amortized) |
int erase(const T& x) |
if x is in the set, deletes it and returns 1 . Otherwise, returns 0 |
\Theta(\log n) |
map<K,V>
A map<K,V>
is a dictionary with keys K
and values V
. It behaves similarly yo a set of pairs (key,value), that is keys are never repeated. By default, the keys are ordered increasingly.4
To use map<K,V>
it is necessary #include <map>
.
An iterator map<K,V>::iterator it
is moved forward using ++it
and backward --it
. To access the pair pointed by it
we use *it
. To access the key pointed by it
we can use (*it).first
or it->first
. To access the value pointd by it
we can use (*it).second
or it->second
.
Method | Cost | |
---|---|---|
pair<map<K,V>::iterator,bool> insert (const pair<K,V>& p) |
add the pair p . If there was a pair with that key, it returns an iterator to the exsiting pair and false . Otherwise, adds the pair p to the map and returns an iterator to it and true |
\Theta(\log n) |
map<K,V>::iterator begin() |
returns an iterator to the pair with the first key in the ordering (by default the smallest key) | \Theta(1) |
map<K,V>::iterator end() |
returns an interator after the pair with the last key in the ordering (by default the largest key) | \Theta(1) |
map<K,V>::iterator find(const K& k) const |
looks for the pair with key k in the map. If it finds it, returns an iterator pointing to it. Otherwise returns end() |
\Theta(\log n) |
void erase(map<K,V>::iterator it) |
deletes the pair pointed by it |
\Theta(1) (amortized) |
int erase (const K& k) |
if there is an element with key k , deletes it and returns 1 . Otherwise, returns 0 |
\Theta(\log n) |
unordered_set<T>
An unordered_set<T>
is like a set
but it does not guarantees an order on the elements.
insert
, find
, erase
work in time \Theta(n) (worst case), but in time \Theta(1) on average.5
unordered_map<K,V>
An unordered_map<K,V>
is like a map
but it does not guarantee an order on the pairs.
insert
, find
, erase
work in time \Theta(n) (worst case), but in time \Theta(1) on average.6
Footnotes
The content of this page is mostly based on STL and Algorithms in C++.↩︎
That is
a<b
is true when eithera.first < b.first
or whena.first == b.first
anda.second < b.second
.↩︎Internally,
set<T>
is implemented with a balanced binary search tree.↩︎Internally,
map<K,V>
is implemented with balanced binary search trees.↩︎Internally,
unordered_set<T>
is implemented with a Hash table.↩︎Internally,
unordered_map<K,V>
is implemented with a Hash table.↩︎