Wednesday, October 14, 2009

C++ STL revisit

I only used a small set of the STL in C++. Here are some re-visit:

Container
  • slist vs. list: list use bi-direction iterator but slist use forward iterator. insertion and splicing will not invalidate the iterator.
  • deque vs. vector: deque supports constant time insertion or removal of the elements at the beginning of the header.
  • map vs. hash_map: map is always implemented using self-balanced search tree, but hash_map is implemented using hash function. map is more appropriate for the element in particular order.
  • set vs. hash_set: similarly, the set is sorted using self-balanced search tree.
  • bitset vs. bitvector: bitset size is fixed and it is not an container at all. 
  • rope: A scalable string implementation for assignment, concatenation, sub string. Single character replacement is expensive.
  • queue, stack is implemented on deque. prority queue is implemented on vector. All of them are container adapters.
Algorithm

     Non-Mutation

  • for_each
  • find_if: The if functor is unary predictor.
  • adjacent_find: You can use a binary predicator. For example, if you want to find the first element that is greater than its successor.
  • count_if: The if functor is unary predictor
  • mismatch: Find the first positions where two ranges differ
  • equal: return true if two ranges are identical
  • search: find a sub-sequence in a range.

No comments: