STL surface written questions summarized (dry) (rpm)

STL face questions written summary

Which components have a .STL?

This provides six STL component can be combined with each other to apply:

1, the container
vessel is a variety of data structures, I do not say, look at this picture below Recall that like, from the implementation point of view, STL container is a class template.

 

2, algorithm
variety of common algorithms, such as sort, search, copy, erase and so on, which I think is more worthy sort, next_permutation, partition, merge sort of learning, from the implementation point of view, STL algorithm is a function template.

3, iterator
play between the container and the glue algorithm, so-called "generic pointer." There are five types, from the implementation point of view, the iterator is a kind of operator *, operator - to be overloaded class template>, operator ++, operator-- pointer and other related operations. All STL containers are shipped with its own dedicated iterator, only the container designers know how to design an iterator. It is also a primary pointer iterators. Is a design pattern, it is asked to understand design patterns can be used to make up the numbers.

4, functor
class behavior function, the algorithm can be used as a strategy, from the implementation point of view, it is a functor overloaded operator () of class or class template. Function pointer can be regarded as generally narrow functor.

5, the container adapter
A method for modifying the container or something functor or iterator interface. For example, queue and stack, looked like container, in fact deque pack a layer of skin.

6, space allocation is
responsible for space allocation and management. From the implementation point of view, the configurator is an implementation of dynamic space allocation, space management, space to release the amount class template.

 

Two .STL common container and what their characteristics are what?

1.vector: the underlying data structure is an array that supports fast random access.

2.list: the underlying data structure is doubly linked list that supports rapid additions and deletions.

3.deque: underlying data structure of a central controller and a plurality of buffers, for details see P146 STL source code analysis, support end to end (not intermediate) rapid additions, supports random access.

4.stack: the bottom layer is generally achieved with a 23, the head can be closed, because no vector should have the capacity size limit, time-consuming expansion

5.queue: 23 implemented with underlying general, the head can be closed because no vector should be limited capacity size, consuming expansion (stack and queue adapter fact, not called container, because the container is then packaged )

6.priority_queue: The underlying data structure is generally the bottom of the container vector, heap stack to process the rules governing the underlying container implementation

7.set: red-black tree underlying data structure, ordered, will not be repeated.

8.multiset: red-black tree underlying data structure, ordered, repeatable. 

9. The Map : Data structure underlying red-black tree, ordered , will not be repeated. (A good article: HTTPS: // blog.csdn.net/sevenjoin/article/details/81943864)

10.multimap: red-black tree underlying data structure, ordered, repeatable.

11.hash_set: underlying data structure is a hash table, disorder, will not be repeated.

12.hash_multiset: underlying data structure is a hash table, disorder, may be repeated.

13.hash_map: underlying data structure is a hash table, disorder, will not be repeated.

14.hash_multimap: underlying data structure is a hash table, disorder, may be repeated. 

 

Third, talk about the underlying std :: vector of (storage) mechanism.

     vector is a dynamic array, which has a pointer to a contiguous memory space, when the data hold enough space, the other will automatically apply a larger piece of space ( typically to increase the current capacity is 50% or 100% ), then the original copy of the data in the past, and then release the original piece of space ; when released inside or delete data, which storage space is not released, only the inside of the empty data .

 

Four, vector insert and delete list What is the difference?

vector insert and delete data, the existing data needs to be copied and moved, if the vector stored object constructor is large or complex, then the overhead is large, if data is simple small, efficient than list.

list insert and delete data, existing data needs to traverse, but insert data in the header, high efficiency.

 

Fifth, under what circumstances a vector, with the list under what circumstances.

vector random storage elements (ie, can be calculated by a formula directly address the elements, without the need to find one by one), but when you insert delete data in a non-tail, low efficiency, suitable for simple objects, objects little change in the number of random access frequently.

list does not support random access memory for large objects, number of objects change frequently, insert, and delete frequently.

 

Sixth, what the vector begin and end the function returns?

begin returns an iterator first element, end is returned behind the last element iterator.

 

Seven, why the vector insert operation could result in failure of the iterator?

vector dynamically increase the size of the original space is not increased after the new space, but at twice the original size in another configuration of a new, larger space, then the contents were copied, and the release of the original space. Since the operation of changing the space, so failure iterator effect.

Eight, to talk about the underlying std :: list of (storage) mechanism.

In the data storage unit of the node, the node address is not necessarily continuous in memory, each insert or delete an element, arranged on or release a space element

 

Nine, list ordering principle comes with the sort function.

The first two elements combined, and the combined the two elements are then combined into two subsequences of the sequence of elements 4, the process is repeated, to give 8, 16, ..., the sub-sequence, and finally to give is sorted sequence.

Time complexity: O (nlgn)

Sort List :: void ()

{

List with Carry;

List counter [64]; // array elements list

int Fill = 0;

the while (! head-> Next = tail)

{

// sentinel head is not stored RMS

/ / head-> next element is removed, it is not required while loop = head- head> Next;

carry.transfer (carry.getHead () -> Next, head-> Next, head-> next-> Next);    

    

int 0 = I;

the while (I <Fill && counter [I] .getHead () -> Next counter = [I] .getHead ()!)

// counter [I] is not empty

{

counter [I] .merge (with Carry) ;

carry.swap (counter [I ++]);

}

carry.swap (counter [I]);

IF (I == Fill) Fill ++;

}

for (int I =. 1; I <Fill; I ++)

counter [I ] .merge (counter [I -. 1]);   

// achieved by this sort (the synthesis of a new ordered list of ordered list)

the swap (counter [Fill -. 1]);

}

Ten, and the vector difference deque.

1) vector space is a continuous unidirectional linear openings, the deque is continuous bidirectional linear space opening . (Bidirectional opening means can be done in the head and tail ends of the elements are inserted and delete operations).

2) deque does not provide space reserved function, but will have to provide a vector space reserved function.

3) deque also provide random access iterators, but it is much more complicated than the vector iterator iterator.

 

XI does not allow traversal behavior of container What (does not provide iterators)?

1) queue, except the head, there is no other way to access other elements of the deque.

2) stack (bottom layer to achieve deque), in addition to the top, without any other means of access to other elements stack.

3) heap, all elements must follow special ordering rule, do not provide traversal function.

 

XII parameter allocate STL container of what to do?

Generally used in the container, as a member of the container, but is generally passed with a template parameter, so that it allows us to replace our custom allocator; STL containers dispenser for packaging low-level details on memory management

 

Thirteen, you understand what an iterator?

Iterator (iterator) for providing a sequential access method for each element a polymeric object, without exposing the internal representation of the object, corresponds to the smart pointer. Including Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, Random Access Iterator.

 

XIV vector after each insert or erase, a previously saved iterator will fail?

In theory fail, theoretically after each insert or erase, all iterators recalculated, it can be seen as a failure of will, in principle, can not use the expired memory
but generally the underlying vector is an array to achieve, we carefully consider the characteristics of the array is not difficult to come to another conclusion,

insert, the insert is assumed in the p position, two situations:
A) the container there is empty space, not reallocate memory, then the previous p iterator is valid, then the iterator will fail p

b) container reallocate memory, then the iterator is not valid after the slightly p

erase, the erase position assumed at p, p before the iterator is valid and the next point to an element position p (p if before the tail, the tail p End points to an invalid), then the iterator p are ineffective

 

Fifteen, STL small memory blocks for request processing and release

STL considering the small memory block debris designed double-level configuration, a first stage configuration directly malloc () and Free (); second stage is disposed as the case of using different strategies, configuration area is larger than when 128bytes of time, a first stage called directly configurator; 128bytes of less than when the arrangement area, it is not by means of a first-stage configuration, used to implement a memory pool. What is the use of a first stage or the second stage is arranged is arranged to control a macro definition. The default SGI STL second stage configurator.
Memory requirements two raised to the configurator will block any small multiple of 8, (e.g., demand is 30bytes, it is automatically adjusted to 32bytes), and it maintains the interior 16 free-list, respectively, each management size 8, 16, 24, ..., 128bytes of small blocks, so that when the demand for a small memory configuration, pulled directly from the free list corresponding to the size of the corresponding memory (multiple of 8); return memory when the client when, inserted into the top of the free list corresponding to the memory blocks according to the size of the return, the return of memory will be required.
Summary:
STL in memory allocator is actually based on the free list (free list) allocation strategy, the most important feature is by organizing 16 free list, the allocation of small objects is optimized.
1) rapid distribution and release small objects. When the disposable preallocated after a fixed size memory pool, the operation of less than 128 bytes of memory allocation and deallocation of small just some basic pointer operations, compared to directly call malloc / free, small overhead.
2) to avoid memory fragmentation. Messy memory fragmentation not only a waste of memory space, memory and OS will put pressure on management.
3) as far as possible to maximize memory utilization. Memory pool when there is insufficient free area allocated to the desired size, which allocation algorithm will links corresponding to the free list, and then attempts to locate a suitable region if the size of the free list,
However, this limited memory allocator STL containers used, it is not suitable for a general memory allocation. Upon release because it requires a memory block, memory block size must be provided in order to determine which to recover the free list, and its object is to know STL containers required size distribution, such as described above:
STL :: Vector Array;
it needs to know the array is allocated object size is sizeof (int). A common memory allocator is no need to know the size of the memory to be released, similar to the free (p).

 

Sixteen, vector and list of differences

Similar vector and array with a contiguous memory space, support random access operation performed in the middle of insertion and deletion time complexity element is O (n)

list is achieved by doubly linked list, data can only be accessed through an array of pointers to traverse an intermediate element, the time complexity is O (n).

 

Seventeen, remove the erase method and vector method of difference algorithn

vector in the erase method actually remove the element iterator can not access the

simply remove the element moved to the rearmost containers, iterators can still be accessed . Because the algorithm is operated by the iterator, do not know the internal structure of the container, it can not really be deleted.

 

Eighteen, STL container What are the advantages and disadvantages?
Tell us about the STL, detailed instructions on how to implement STL vector.

The STL (Standard Template Library) can be divided into the container (containers), iterators (iterators), space configurator (allocator), adapter (adapters), six parts algorithms (algorithms), functor (functors).
. STL (Standard Template Library, Standard Template Library which consists of containers, algorithms, iterators.
: STL The following are some of the advantages of
a family of algorithms for sorting data can easily search for data or easily achieved;
safer and debugging programs easy;
even people with STL written in UNIX platform code you can easily understand (because STL is cross-platform).
the Vector is essentially a dynamic array, based on the increase of data, dynamic arrays to increase space.
from the logical point of view deque continuous memory, essentially a section of contiguous space is composed of a fixed size .deque index structure similar memory management functions .vector have capacity and reserve, and deque list, there is no reserve capacity and function.

Summarized as follows: 
1. Insert or Delete all will iterator deque element to reference in the middle deque, pointer failure 
2. Failure in the head or the tail deque insert elements make iterators, pointers and references but will not cause failure 
3. its head or tail will only remove elements point to the deleted element iterators failure

vector template consecutive array data in memory, the random access elements (i.e. by [] operator access) is the fastest, and it is consistent with the array. Also because of its continuously arranged, it is added or deleted at a position other than the element of the tail is slow, when using Vector, to avoid this operation.
Template data list is a chain store, it is not a random access element. It has the advantage of speed add remove elements anywhere.
deque template links through a plurality of successive data pieces realized, the equilibrium characteristics of two or more containers

 

Ninth, talk about the underlying mechanism of std :: deque.

deque dynamically combination piecewise continuous space, ready Add a new continuous space and linked. It does not provide a space reserved function.

Note: Unless necessary, we chose to use vector rather than deque as possible because deque iterators much more complicated than vector iterators. Deque sort of, in order to improve efficiency, you can simply copy the deque to sort on a vector, and then copied back deque.

deque uses a map (not map the STL containers) as a master, which is a small continuous space, wherein each element is a pointer to another section larger continuous space (buffer).

deque iterator contains four elements:

1) cur: current element pointed to by the iterator

2) first: the first buffer referred to this iterator.

3) last: end of the buffer.

4) node: directivity control center.

 

XX, talk about std :: map the underlying mechanisms.

RB-TREE to map to the underlying mechanism. RB-TREE is a balanced binary search tree, automatic sorting good results.

Can not change its key through the iterator map can only modify fact value. So the map is neither a const iterator is not mutable.

 

XXI red-black tree what nature?

1) Each node is red or black.

2) root is black.

3) black leaf node NULL node.

4) If the node is red, its child nodes must be black.

5) according to any one node to any path of NULL, it must be the same number of nodes contained in the black.

 

Twenty-two, vector, list, map, after using erase (it), change the iterator deque.

is the sequence vector and deque container, which are continuous memory space and piecewise continuous space, delete the iterator it, behind iterators are ineffective, and this time it will automatically later iterations is incremented by 1 so that it points to the next element is removed elements.

When the list iterator delete it, behind the iterator will not fail, you can connect the front and back.

map also can delete the current iterator fails, its later iterations is still valid.

 

Twenty-three, hash_map difference between the map? When to use hash_map, when to use map?

Constructor: hash_map need hash function and function equal to, the need to map comparison function (greater than or less than).

Storage structure: hash_map hashtable to as the bottom, and to map the bottom of RB-TREE. 

Overall, hash_map faster search speed than the map, but also to find the basic speed regardless of the magnitude and the amount of data belonging to a constant level. The search speed map is logn level. But it is not necessarily constant smaller than the log, but also hash function hash_map time-consuming.

If you consider the efficiency, especially when the element reaches a certain magnitude, with hash_map.

When considering the memory, or a small number of elements, with the map.

 

Twenty-four, hashtable hash_set, difference, hash_map of.

hash_set to hashtable to bottom, does not have a sorting function, can quickly find. Its value is the real value. (Set to RB-TREE is a bottom, having a sorting function.)

hash_map order to hashtable to the bottom, there is no automatic sorting function, you can quickly find, each element also has a real value and values. (Map to RB-TREE is a bottom, having a sorting function.)

 

XXV, three issues and set the map.

1) Why insertion and deletion efficiency map and sequence set higher than other containers.

Because no memory copy and move memory

2) Why map and set after each Insert, previously saved iterator will not fail?

Since only node pointer insert operation in exchange for going, the memory node is not changed. Like the iterator node pointer, the memory has not changed, a pointer to memory does not increase .

2) When the increase in data element (from 10,000 to 20,000), search speed set of map of what will change?

RB-TREE with binary search method, time complexity is logN , increased from 10000 to 20000, the number of lookups to log10000 = 14 times log20000 = 15 times, more than 1 time only.

 

Twenty-six, map is how to achieve? Find the complexity is the number? You can traverse the side edge insert?

Red-black tree

O (logn)

Not, map Vector like, it does not return after performing the erase operation on a container element iterator can not be traversed back when deleted.

 

Xxvii, where the difference in hash_map and map?

hash_map flat bottom is theoretically operated so hash average time complexity is constant , underlying map is a red-black tree, in theory, the average complexity is O (logn), summarized here, or hash_map selected map, the key is the key to look word queries operations, and you need to ensure that the overall time is the time a query or a single query. Many times if it is to operate, requires its overall efficiency, use hash_map, the average processing time is short. If the operation is a few times, may cause uncertainty hash_map using O (N), then the average processing time is relatively slow, single treatment time constant map, consideration should be higher than the overall stability of the overall efficiency, because the premise fewer number of operations. If a process using a small number of operations to produce a hash_map worst case O (N), then hash_map the advantage thus ruined.

 

Xxviii, why can not the same map, and set to have a reserve function to pre-allocate as vector data?

map and a set of internal storage is not the element itself, but rather contains the node elements. That is not the internal Alloc map statement using the map when passed from parameter Alloc. For example:
Map, Alloc> intmap;
the allocator is not in use Alloc intmap this time, but rather by the Alloc conversion through internal conversion DETAILED
Alloc :: rebind redefines the new node dispenser, detail see the complete implementation of learning the STL Allocator.
In fact, you will remember that in the map and set the dispenser inside has changed, reserve method you do not wish for.
----------------
Disclaimer: This article is CSDN blogger original article "zzb2019", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/zzb2019/article/details/81195294

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11821323.html