C ++ standard library analysis summary (two) - <template, distributor>

And the relationship between the classification This section summarizes the classification templates and class templates and STL inside the dispenser, the internal structure of container and container

1, the source code version of the presentation

  1.1 VC compiler source code directory:

2, class template

  2.1 Introduction class template:

  2.2 class template Category:

  Generalization (GP Generic Programming), specialization (Specialization), except that the two specialized version compared to the efficiency optimization generalized versions may exist

  2.3 specializations (partial specialization Partial Specialization)

    • Specialization also called all the specialization, the above example is only one class template parameters, in fact, can consist of multiple, all of this specialization is for all parameters are special bindings;
    • Partial specialization is called localized specialization, for example, a plurality of the following parameters wherein only a portion of the binding (or binding local number within the local range).

  

3, function template

  3.1 Introduction to function template:

4, distributor

4.1 allocate memory in real terms

  1. Dispensers are the unsung heroes of the container, use is responsible for memory, but it is not recommended to use it directly, because there is no need to use it, it is designed for container services, the distributor can understand better grasp of the operational efficiency of container to help us selection of suitable container;
  2. Essence distributor operating memory or call the C Runtime Library malloc and free (it can be said of the new c ++ and any other upper memory operation mode is the final layers of calls to malloc and free return of these two functions), two functions depending on the platform and then again to call their System API.
  3. The memory system applications is as follows

  Therefore, the system allocates memory for each application to us is far greater than our actual space application, because this memory system to give some additional identification (also known cooking or overhead) to facilitate the release operation, because the additional part is fixed, we will say that the more memory the application, the smaller the proportion of the additional part of the occupied.

4.2 distributor source code implementation

Note: If the element every time we put the container is too small, for example, put a long (4 bytes), additional overhead that will be a great memory to produce, if put 1 million, and that overhead than you put 1 million long even more, memory consumption will be very scary.

 Use 4.3 distributor

  由此可见,分配器的使用在释放时需要我们自己去记得当初申请了多少内存,显然是不方便的,但是我们在使用容器时不用关心,结论就是个人慎用。

4.4 分配器工作模式

   用分配器而不直接用C RunTime Library提供的malloc函数,其本质上是减少内存的额外开销,也就是说分配器会预先开好一定的空间,尽量减少malloc的使用次数,它不是需要多少开多少,也就是我们常说的内存池设计。

5、 容器的结构分类介绍

5.1 各个容器的结构关系

 

 

 

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/laiyingpeng/p/11181320.html