12 January, 2009

Book: Effective STL

Recently I read a good book about STL, "Effective STL". I made a simple summary about this book.
The following list are the items which I think are important and I did not know most of them at first.

Item 4. Call empty instead of checking size() against zero
You should prefer the construct using empty, and the reason is simple: empty is a constant-time operation for all standard containers, but for some list implementations,
size takes linear time.

Item 5. Prefer range member functions to their single-element counterparts.
It is definitely faster, but be careful with the pointer.

Item 6. Be alert for C++'s most vexing parse.

Item 7. When using containers of newed pointers, remember to delete the pointers before the container is destroyed.
One thing you must never be fooled into thinking is that you can arrange for pointers to be deleted automatically by creating containers of auto_ptrs.

Item 8. Never create containers of auto_ptrs.

Item 9. Choose carefully among erasing options.
use earse: vector, dqueue, associative container
use remove: list

Item 17. Use "the swap trick" to trim excess capacity.
vector(contestants).swap(contestants);

C++ guarantees that local objects are destroyed if an exception is thrown

Item 23. Consider replacing associative containers with sorted vectors.

"maps and multimaps keep their elements in sorted order, but they look only at the key
part of the element (the first component of the pair) for sorting purposes, and you must
do the same when sorting a vector. You'll need to write a custom comparison function
for your pairs, because pair's operator< looks at both components of the pair."

"Interestingly, you'll need a second comparison function for performing lookups. The
comparison function you'll use for sorting will take two pair objects, but lookups are
performed given only a key value. The comparison function for lookups, then, must
take an object of the key type (the value being searched for) and a pair (one of the
pairs stored in the vector) — two different types. As an additional twist, you can't
know whether the key value or the pair will be passed as the first argument, so you
really need two comparison functions for lookups: one where the key value is passed
first and one where the pair is passed first."

Item 29. Consider istreambuf_iterators for character-by-character input.

Item 30. Make sure destination ranges are big enough.
Use inserter: front_inserter,inserter,back_inserter

v.erase(remove(v.begin(), v.end(), 99), v.end());

Item 41. Understand the reasons for ptr_fun, mem_fun, and mem_fun_ref.

Cite form the Wikipeida:
Deques are often implemented using a variant of a dynamic array that can grow from both ends, sometimes called array deques. These array deques have all the properties of a dynamic array, such as constant time random access, good locality of reference, and inefficient insertion/removal in the middle, with the addition of amortized constant time insertion/removal at both ends, instead of just one end. Two common implementations include:

* Storing deque contents in a circular buffer, and only resizing when the buffer becomes completely full. This decreases the frequency of resizings, but requires an expensive branch instruction for indexing.
* Allocating deque contents from the center of the underlying array, and resizing the underlying array when either end is reached. This approach may require more frequent resizings and waste more space, particularly when elements are only inserted at one end.

No comments: