This is an old revision of the document!
Rule of Three
Rule of Five
STL containers
The STL library has 3 types of container classes:
- sequence containers - container classes that maintain the ordering of elements in the container. Examples are vectors, deques, and lists.
- associative containers - container classes that automatically sort their inputs when the inputs are inserted into the container. Eg. are a set, multiset, map or multimap.
- container adapters - special predefined containers that are adapted to specific uses, such as a LIFO stack or a FIFO queue.
Sequence Containers
Vector
An std::vector is a sequence container dynamic array that can grow to add to more elements.
Deque
An std::deque is double ended array that can grow from each end?
List
An std::list is a sequence container where each element contains a pointer to the next and previous element. You can't randomly access elements, you have to “walk the list”. But inserting elements is very fast if you know where to insert them.
Set
An std::set is an ordered list of unique elements that get automatically sorted as we insert them.
Multiset
An std::multiset is an ordered list of elements that can contain duplicate data which is automatically sorted as we insert them.
Map
An std::map is an associated array which is a map, symbol table or dictionary that has a collection of key value pairs such that a key only shows up once.
Memory Leaks
For every new there must be a delete. Use the following valgrind command to profile for mem leaks1):
valgrind --tool=memcheck --leak-check=yes name_of_exec