STL- source analysis, a brief summary

STL six components function and application

1. container

Various data structures, vector, list, deque, set , map, from the implementation point of view, STL container is a class template

2. Algorithms

Variety of commonly used algorithms, sort, search, copy, erase , from the implementation point of view, STL container is a function template

3. iterator

Plays glue between the container and algorithms, generic pointer , there are five types, from the implementation point of view, is the operator *, operator - to be overloaded class template>, opreator ++, opreator- pointer and other related operations, each a STL containers are shipped with its own dedicated iterators. It is also a primary pointer iterators.

4. functor

What is a functor?
Behavior similar function. A good experience at this sentence

The adapter (Adapters)

As the name suggests, think about the mode adapter (adapter design)

6. configurator (allocators)

Responsible for space allocation and management, to achieve a dynamic space allocation, space management, space is freed.


Space configurator (the allocator)

Here Insert Picture DescriptionHere Insert Picture Description
SGI designed the double-level configuration is (the interview will be asked), mainly to solve the problem of broken small memory blocks that might be caused.
SGI first stage configuration by malloc and free to complete the configuration and release of memory. The second stage is arranged to operate by different strategies: First, look at the size of the arrangement area to see if it exceeds 128bytes of, it is, to a first-stage configuration, a configuration when the tile is 128bytes of less than, using the memory pool finishing way.
So, what is only the first stage on OK, or both at the same time to use it, depending on whether the __USE_MALLOC this parameter is defined. (Hou Jie teacher here Note: that this parameter name is not ideal, because in any case always malloc to be called, right). FIG look specific process.
Here Insert Picture Description


Explained one by the first stage and a second stage arranged below configurator

The first stage configurator :

Implementation mechanism similar to the C ++ new-handler can not directly use this mechanism, because he was not configure memory usage operator new.

So what is the new-handler mechanism it, you can ask the system memory configuration when demand can not be satisfied, you call a specified function, in other words, when the memory configuration does not help, you can make your system perform specified function.

:: operator new that is not fulfilled the task before the throw std :: bad_alloc abnormal state, will first have to call the client is handling routines you specify, the processing routine is often called new-handler.

Hou teacher asked a few more here: that design and set "out of memory handling routine" is the responsibility of the client, there is a specific routine

第二级配置器:
小于128bytes时,以内存池管理(memory pool),该方法又称为(sub-allocation);每次配置一大块内存,并维护对应之自由链表(free-list)。
有16个free-lists,各自管理不同大小的小额区块,例如8,16,32,
每次配置一大块内存,并维护对应的free list,若下次还是相同大小的内存需求,直接从free-list中拨出。

  1. allocate()函数 ,该函数,首先判断区块大小,大于128,嗯,你懂得,小于的话,就看对应的free-list。如果free-list 内有能用的区块,就直接拿来用,没有的话,将区块上调至8倍数边界,然后调用refill()函数,准备为free-list重新填充空间。
  2. dellocate()函数,同样的,首先判断区块大小,大于128,小于128,就找到对应的free-list ,将区块回收。
  3. chunk_alloc() 函数,从内存池中取空间给free-list,首先会判断水量够不够,够的话,就取出20个区块给free-list,不够20个区块,还可以供应一个以上的话,就把这些区块拨出去,问题来了,假如一个都不够的话,该怎么办,对客户端显然无法交代,对不对,通过malloc()从heap中配置内存,为池子里注入新的水以供使用。新水量为需求的两倍,再加上一个随着配置次数增加而越来越大的附加量。
    Here Insert Picture DescriptionHere Insert Picture Description
    Here Insert Picture Description

内存池(面试题)


迭代器(iterators)概念与traits编程技法

STL 的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以一贴胶水将两者联系在一起。

迭代器是一种smart pointer

序列式容器

vector

vector 与array 的区别是什么呢?
array 是静态空间,一旦配置了就不能改变,vector是动态空间。vector数据空间是连续线性空间,为了降低空间配置是的速度成本,vector实际配置的大小可能比客户端需求量更大一些。

vector 动态增加大小,并不是在原来的空间之后接续新空间(因为无法保证原始空间后面是不是还有可以分配的空间),而是以原大小的两倍另外配置一块较大的空间,然后将原内容拷贝过来,然后才开始在原内容之后构造新元素,并释放原始空间。

vector 为单向数组,只能pushback 和pop back
Here Insert Picture Description

腾讯面试题:通过三个迭代器:start,finish,end_of_storage 完成内存分配。


list

list 有个一个prev 和next ,所以为双向链表,而且还是环装双向链表


deque

deque是一个双向开口的连续线性空间。可以在头尾两端分别做元素的插入和删除操作。

deque 的 迭代器的比较复杂相比于vector
因此,除非必要,我们应尽可能选择使用vector,而非deque,例如,对deuqe进行排序操作,为了高效,可以将deque先完整复制到一个vector,将vector排序后(利用STL sort 算法),在复制回deque。

deque 数据结构 采用所谓map 作为主控,map是一小块连续空间,其中每个元素(此处叫node,节点)都是指针,指向另一段(较大的)连续线性空间,称为缓冲区。缓冲区才是deque 的存储空间主体。我们可以指定缓冲区的大小。默认为512bytes。

Here Insert Picture Description


stack

以某种既有容器作为底部结构,将其接口改变,让其满足“先进后出”的特性,形成一个stack,是很容易做到的。

Here Insert Picture Description

queue

Here Insert Picture Description

heap

Here Insert Picture Description

priority_queue

Here Insert Picture Description

slist

Singly linked list

Associative containers

RB-tree

Here Insert Picture Description


set

The underlying data structure is a RB-tree

Face associative containers, should use the find function to search for the elements they provide more efficient

itel = iset.find(3);          //查找3这个元素 而不是用find(first,last,value) 

map

RB-tree tree is the underlying <key, value>

multiset & multimap

Multiset and usage characteristics and the same set, the only difference is that it allows duplicate keys, map and multimap is true

hash_set & hash_map & hash_multiset & hash_multimap

Underlying all hashtable

algorithm

int a[] = {0,1,2,3,4,5,6,6,6,7,8}


for_each(begin(),end(),display<int>());  // 可以用来打印  
adjacet_find(begin(),end());// 找到相邻元素值相等的第一个元素 ,这里为6
adjacet_find(begin(),end(),equal_to<int>());// 找到相邻元素值相等的第一个元素 ,这里为6
count(begin(),end(),6) ;// 数组中值为6的个数
count_if(begin(),end(),bind2nd(less<int>()),7); // 找出小于7的元素个数 
find_if(begin(),end(),bind2nd(greater<int>()),2)// 找出第一个大于2的元素的位置

Note: If the next day, I'll be here to tag, write the algorithms used

Functor (another name function object function objects)

//用户继承 binary_function,便可以取得该仿函数的各种相应的类型
eg:
template<class T>
struct plus :public binary_function<T,T,T>{
    T  opreator()(const T& x,const T& y) const {return x+y}
}

Adapter

Guess you like

Origin blog.csdn.net/shaoye_csdn1/article/details/90481312