EECS 211 STL Summary PDF
EECS 211 STL Summary PDF
Home Lectures
Class Info Newsgroup
Links Assignments
STL Summary
This is a brief summary of the containers in the C++ Standard Library (once
informally known as the Standard Template Library or STL). It deliberately sacrifices
completeness for simplicity. Information is also available on the iterators and
algorithms.
In my opinion, the best overall reference to the Standard Library is Josuttis' The C++
Standard Library.
vectors
lists
deques
stacks
queues
priority queues
sets and multisets
maps and multimaps
pairs
Vector
Header
#include <vector>
Constructors
Accessors
1 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
Modifiers
O(1)
v.push_back(value) Add value to end.
(amortized)
v.insert(iterator, Insert value at the position indexed by
value) O(n)
iterator.
v.pop_back() Remove value from end. O(1)
v.erase(iterator) Erase value indexed by iterator. O(n)
v.erase(begin, end) Erase the elements from begin to end. O(n)
Deque
Header
#include <deque>
Constructors
Accessors
Modifiers
2 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
O(1)
d.push_front(value) Add value to front.
(amortized)
O(1)
d.push_back(value) Add value to end.
(amortized)
d.insert(iterator, Insert value at the position indexed by
value) O(n)
iterator.
d.pop_front() Remove value from front. O(1)
d.pop_back() Remove value from end. O(1)
d.erase(iterator) Erase value indexed by iterator. O(n)
d.erase(begin, end) Erase the elements from begin to end. O(n)
List
Header
#include <list>
Constructors
Accessors
Modifiers
3 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
Stack
In the C++ STL, a stack is a container adaptor. That means there is no primitive
stack data structure. Instead, you create a stack from another container, like a list,
and the stack's basic operations will be implemented using the underlying container's
operations.
Header
#include <stack>
Constructors
Accessors
Modifiers
Queue
In the C++ STL, a queue is a container adaptor. That means there is no primitive
queue data structure. Instead, you create a queue from another container, like a list,
and the queue's basic operations will be implemented using the underlying
container's operations.
Header
#include <queue>
Constructors
4 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
Accessors
Modifiers
Priority Queue
In the C++ STL, a priority queue is a container adaptor. That means there is no
primitive priorty queue data structure. Instead, you create a priority queue from
another container, like a deque, and the priority queue's basic operations will be
implemented using the underlying container's operations.
Priority queues are neither first-in-first-out nor last-in-first-out. You push objects
onto the priority queue. The top element is always the "biggest" of the elements
currently in the priority queue. Biggest is determined by the comparison predicate
you give the priority queue constructor.
Header
Constructors
Accessors
5 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
Modifiers
Sets are implemented with balanced binary search trees, typically red-black trees.
Thus, they provide logarithmic storage and retrieval times. Because they use search
trees, sets need a comparison predicate to sort the keys. operator<() will be used by
default if none is specified a construction time.
Header
#include <set>
Constructors
Accessors
s.find(key)
Return an iterator pointing to an occurrence of key in s, or O(log
s.end() if key is not in s. n)
s.lower_bound(key)
Return an iterator pointing to the first occurrence of key in O(log
s, or s.end() if key is not in s. n)
Return an iterator pointing to the first occurrence of an
O(log
s.upper_bound(key) item greater than key in s, or s.end() if no such item is
n)
found.
O(log
s.equal_range(key) Returns a pair of lower_bound(key) and upper_bound(key).
n)
6 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
O(log
s.count(key) Returns the number of items equal to key in s.
n)
s.size() Return current number of elements. O(1)
s.empty() Return true if set is empty. O(1)
s.begin() Return an iterator pointing to the first element. O(1)
s.end() Return an iterator pointing one past the last element. O(1)
Modifiers
Maps are implemented as sets of pairs of keys and values. The pairs are sorted based
on the keys. Thus, they provide logarithmic storage and retrieval times, but require a
comparison predicate for the keys. operator<() will be used by default if none is
specified a construction time.
Map types are a bit complicated because of the pairs, so it's best to use typedef to
create more readable type names, like this:
typedef map<string, double> ValueMap;
typedef ValueMap::value_type VMPair;
typedef ValueMap::iterator VMIterator;
Definitions like the above will make find() and insert() a lot simpler:
ValueMap vm;
vm[ "abc" ] = 2.0;
vm[ "def" ] = 3.2;
vm.insert( VMPair( "ghi", 6.7 ) );
You can just use map[key] to get the value directly without an iterator.
Warning: map[key] creates a dummy entry for key if one wasn't in the map
7 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
before. Sometimes, that's just what you want. When it isn't, use find().
multimaps are like map except that they allow duplicate keys. map[key] is not defined for
multimaps. Instead you must use insert() to add entry pairs, and find(), or
lower_bound() and upper_bound(), or equal_range() to retrieve entry pairs.
Header
#include <map>
Constructors
Accessors
m[key]
Return the value stored for key. This adds a default value if O(log
key not in map. n)
m.find(key)
Return an iterator pointing to a key-value pair, or m.end() O(log
if key is not in map. n)
m.lower_bound(key)
Return an iterator pointing to the first pair containing key, O(log
or m.end() if key is not in map. n)
m.upper_bound(key)
Return an iterator pointing one past the last pair O(log
containing key, or m.end() if key is not in map. n)
Return a pair containing the lower and upper bounds for
O(log
m.equal_range(key) key. This may be more efficient than calling those
n)
functions separately.
m.size() Return current number of elements. O(1)
m.empty() Return true if map is empty. O(1)
m.begin() Return an iterator pointing to the first pair. O(1)
m.end() Return an iterator pointing one past the last pair. O(1)
Modifiers
O(log
m[key] = value Store value under key in map.
n)
m.insert(pair)
Inserts the <key, value> pair into the map. Equivalent to the O(log
above operation. n)
Pair
A pair is a bit like a Lisp CONS cell. It holds just two values. They can be different
8 of 9 3/28/2010 8:02 AM
EECS 211: STL Summary http://www.cs.northwestern.edu/academics/courses/211/html/stl-summary...
types. For simplicity, pairs are simple generic struct's with two public data members:
first and second and a simple constructor that takes the two values to store.
Header
#include <utility>
Constructors
pair< first_type, second_type > p( first, Makes a pair. Both values must be
second ); O(1)
given.
pair< first_type, second_type > p( pair ); Makes a pair from another pair. O(1)
Accessors
Modifiers
9 of 9 3/28/2010 8:02 AM