Never try to expect all the container has the same interface. Even a generic
erase(), for sequence, It return next iterator,(because it will invalid the
iterator). But for associative, it return void(c++ 98) and return next
iterator(C++ 11). If you just want to change a container in the future, you
should put a container into a class: CustomCollection. then hide it
from the class client. The detail can be seen in Effective STL item
2.
The standard associative container are implemented as balanced binary
search trees. It’s optimized for a mixed combination of insertions, erasures
and lookup. But if that is dictionary, It can be fall into three distinct phases.
setup, lookup, modify. and modify is not happen very often. lookup is very
often. At this time, associative container is not best option. sorted vector
also support log search time. 1) It use less space. 2) more space cause page
fault in memory, then the same log search complex, but sorted vector is
faster than associate container. For dictionary, please use sortedvector.
Avoid using vector<bool>, use deque<bool> or bitset
item 22 Avoid in-place key modification in set and multiset
you can’t change key in map because it’s const default.
Item19 effective STL, Understand the difference between equality and
equivalence in associative Containers.
equality is based on operator ==. equivalence is based on
operator<. Because associate container, set, map, they must
sort their elements, so they must use operator<. Then it use
!if(a<b)&&!if(b<a) to define equivalence, and associate container
use equivalence to decide if a object exist in container.
if you don’t have custom compare funciton, most of time
equivalence is equal to equality, but if you define you specific
compare function, you need to know below:
It will cause container.find and generic algorithm find has different
result
It will lead to item 21 in effective STL. Always have comparison
functions return false for equal values. (strict weak ordering
)
It will lead to item 20 in effective STL. For associative container
of pointers, You need to specify comaprsion types, You want
to order by pointers or want to order by objects pointed by
pointer.(Most of time, the second option is what we want)