Standard Template Library
The C++ STL provides four components:
- algorithms
- containers
- iterators
- functions
Containers
Containers store data with specific structure:
- standard STL sequence containers : vector, string, dequeue, list
- standard STL associative containers : set, multiset, map, multimap
-
standard non-STL containers : bitset, valarray, stack, queue, priority_queue
- list should be used when there are frequent insertions and deletions from the middle of the sequence.
- deque should be used when most insertions and deletions take place at the beginning or at the end of the sequence.
Contiguous-memory containers
- Container in which more than one element is stored in the allocated chunk of memory. If an element in the chunk is inserted or deleted, the rest of the elements need to be shifted up or down in the memory.
- Ex: vector, string, dequeue
Node-based containers
- Containers that can allocate one element per chunk.
- Ex: set, list
Item: Encapsulation
While writing containers try to encapsulate things:
Instead of the above code, this code makes more sense:
The above code is more robust to the changes made to the container late on.
Item: empty() vs size()
It seems like checking for empty and size against zero should be the same.
One should always prefer empty()
operation because:
- Its generally implemented inline, which checks if size is 0.
- It takes constant time. In case of
list
, thesize()
takes linear time.
Item: range operators
Use assign()
operator to assign values to container.
To assign v2
’s half content to v1
:
The above code does not require any loop. It takes the range from iterators as input.