Personal tools
User menu

Knowledge Base/CPP/Effective STL

From Thalesians

Jump to: navigation, search

This is essentially the table of contents of Scott Meyers' book Effective STL. The same table of contents is available on, but not on (check out the "Look Inside" feature). In our view, every serious C++ programmer should read Scott's book. Of course, you need to read the entire book. The chapter titles will only make sense if you have read it. If you haven't read it and the titles still make sense, you are still missing a lot. We would read the entire book.



  1. Choose your containers with care
  2. Beware the illusion of container-independent code
  3. Make copying cheap and correct for objects in containers
  4. Call empty instead of checking size() against zero
  5. Prefer range member functions to their single-element counterparts
  6. Be alert for C++'s most vexing parse
  7. When using containers of newed pointers, remember to delete the pointers before the container is destroyed
  8. Never create containers of auto_ptrs
  9. Choose carefully among erasing options
  10. Be aware of allocator conventions and restrictions
  11. Understand the legitimate uses of custom allocators
  12. Have realistic expectations about the thread safety of STL containers

vector and string

  1. Prefer vector and string to dynamically allocated arrays
  2. Use reserve to avoid unnecessary reallocations
  3. Be aware of variations in string implementations
  4. Know how to pass vector and string data to legacy APIs
  5. Use "the swap trick" to trim excess capacity
  6. Avoid using vector<bool>

Associative Containers

  1. Understand the difference between equality and equivalence
  2. Specify comparison types for associative containers of pointers
  3. Always have comparison functions return false for equal values
  4. Avoid in-place key modification in set and multiset
  5. Consider replacing associative containers with sorted vectors
  6. Choose carefully between map::operator[] and map::insert when efficiency is important
  7. Familiarise yourself with the nonstandard hashed containers


  1. Prefer iterator to const_iterator, reverse_iterator, and const_reverse_iterator
  2. Use distance and advance to convert a container's const_iterators to iterators
  3. Understand how to use a reverse_iterator's base iterator
  4. Consider istreambuf_iterators for character-by-character input


  1. Make sure destination ranges are big enough
  2. Know your sorting options
  3. Follow remove-like algorithms by erase if you really want to remove something
  4. Be wary of remove-like algorithms on containers of pointers
  5. Note which algorithms expect sorted ranges
  6. Implement simple case-insensitive string comparisons via mismatch or lexicographical_compare
  7. Understand the proper implementation of copy_if
  8. Use accumulate or for_each to summarise ranges

Functors, Functor Classes, Functions, etc.

  1. Design functor classes for pass-by-value
  2. Make predicates pure functions
  3. Make functor classes adaptable
  4. Understand the reasons for ptr_fun, mem_fun, and mem_fun_ref
  5. Make sure less<T> means operator<

Programming with the STL

  1. Prefer algorithm calls to hand-written loops
  2. Prefer member functions to algorithms with the same names
  3. Distinguish among count, find, binary_search, lower_bound, upper_bound, and equal_range
  4. Consider function objects instead of functions as algorithm parameters
  5. Avoid producing write-only code
  6. Always #include the proper headers
  7. Learn to decipher STL-related compiler diagnostics
  8. Familiarise yourself with STL-related web sites
  • This page was last modified on 13 January 2009, at 18:50.
  • This page has been accessed 10,205 times.