STL container: sequence container, associative container and containter
adapter:
sequence: vector, list, string, deque
associative: map , multiset , set and multimap
adapter: stack, queue and priority_queue.
If you can find same name member functions in container,prefer to use member function than generic algorithm.Such as sort, merge, remove,reverse, unique in list, andfind, count, low_bound in set and map.
all container support empty(), size(), max_size(), and swap().
max_size() is just theoretic value. (4 functions)
First class container includes begin(), end(), rbegin(), rend().
insert() and erase(). (6 functions)
sequence container support pop_back(), push_back(),front() and
back() (4 functions) while associative don’t.
vector support operator[], list support push_front and pop_front.
and deque support both. (3 function.) By now deque
support (4+6+4+3 = 17 functons)container(4)–> firstcontainer(6)–>sequence container(4)–>deque(3)
vector support reserve, list has reverse(not reserve) and splice().
deque support nothing.
list support it’s own version sort(), remove(), merge(),unique().
For other container, you can use the generic algorithm, Why
list has its own? For sort(), list doesn’t support random
access iterator. For merge(), remove() and unique(). generic
algorithm just use copy method, but list has high efficient
pointer implementation, So list offer its own version merge(),
remove(),sort(), unique().
All first class container support begin() and end(). Only sequence
containers support push_front() or push_back(). begin is notequal front, begin can be used for all first class container,but front only be used for sequence container.
Associative container support It’s own
find(), count(), lower_bound(), upper_bound() , equal_range().
They share the same name with generic algorithm, Don’t confuse
them. When you deal with associative container, just use container
member function, don’t use generic algorithm. Associative offerlog-time lower_bound, upper_bound and equal_range,but generic algorithm just linear time.
deque is the data structure of choice when most insertions and
deletions take place at the beginning or at the end of the sequence.
It doesn’t not guarantee continuity within memory, and higher
constant factor cost than vector. Although both offer random
access to elements and linear-time insertion and removal from
middle of a sequence, the vector is faster. In my evernote, you can
see a implementation of deque.
For stack and queue default use deque as container inside. Why,
because for a large amount of element, vector has relocation
problem, each relocation need to copy and move all the old
elements. For example, if vector has 1000, It probably need
log(1000) = 10 times relocation. that is a little costly. On the
contrary, deque just allocate a new place then insert pointer of
new place to pointer map. It’s relatively cheap. Of course, if you
are quite sure that you stack will just contain 10 items, you can
pass vector as inside container too. That is why it call "container
adapter"
All the container adapter support push(), pop(). and stack and
priority_queue support top(). They don’t have any iterator.