I used C++ couple years already, but never take a close look at it. when looking back to few C++ demo works at school, I had a strong feeling at that time, to make the code running is my only goal, how it work or how to improve it, is always ignored, I am afraid in the language details, as the wisdom say: the evil is in details.
after working for three years, I feel the necessary and urgent to back to the language itself(e.g. Linux, C/C++) as the first step to move forward in application development. Frameworks, engineering-based APIs are more close to final products, making them easy to be attracted, compared to how the details implemented. like the mechanical undergradute students, who first-time run ABAQUS with beatiful visulized results, feels so good.
anyway I have to delay the short satification or self-feeling-good. C is clean and the applications have clear structure, C++ is more metaphysics, I even don’t know where to start. even I thought I knew C++ well, but actualy there are many details behind, e.g. allocator in STL.
template
template is used for generic programming, e.g. both vector
function template
|
|
the actual meaning of TYPE is deduced by compiler depending on the arg passed to this function. “class” or “typename” is similar.
template supports default parameters, once the i-th argument is set with default value, all arguments after it must using default values.
class template
|
|
feel the power of template in STL source code.
STL containers
references:
<< the annotated STL source using SGI STL >> by jjHou
a visitor guide to C++ allocator
the annotated STL source
I took several days to start, cause the first section on allocator already blocked me.
why allocator ?
The logic of a dynamic container doesn’t depend on the specifies of how memory is obtained; namely, the design of container classes should be decoupled from the memory allocation policy.
how allocator works ?
memory allocation and object construction is separated, allocator has four operations: allocate(), deallocate(), construct(), destroy(). there are std interface to allocators
|
|
WHEN we say A is an allocator for T , where T is a type e.g. AllocatTraits::value_type. we mean, A knows how to obtain and release memory to store objects of type T. git:allocator
iterator
iterator is used to build algorithms in containers. while I don’t really get the traits
|
|
vector
std::vector is dynamic, continous.
|
|
list
std::list is cycling double-direction link list.
|
|
dequeue
std::dequeue can operate elements at both ends, and the memory is multi-sectional, in each memory section is linear continous, with advantage of vector and list.
|
|
stack
std::stack only operate on the top element (first in last out), can’t iterate the container, the default container for stack is deque.
|
|
queue
std::queue supports only pop element from front, and push element into end, the default container is dequeue
|
|
priority queue
std::priority_queue is queue with priority.