C ++ cow brush off questions day3

1: map and set of differences:

and C ++ are set map associated container, the underlying implementation is black trees (RB-Tree). As the map and set the various operations open interfaces, RB-tree are also provided, so almost all of the map and set of operational behavior, just transpose the operational behavior of RB-tree.

map and set differences:

(1) map the elements are key-value (key - value): functions index key, the index value is associated with the data representation; the Set contrast is simply a collection of keywords, the SET each element contains only one keyword.

(2) set to a const iterator is not allowed to modify the value of the element; Map allows modification value, but not modify key. The reason is because the map is set based on keyword rankings and to ensure its orderly, if allowed to modify the key, then you first need to delete the key, and then adjust the balance, and then insert the key modified to adjust the balance, so a to seriously undermine the structure of the map and set, leading iterator failure, do not know should point to the position before the change, or point to the location after the change. Therefore iterator will set the STL arranged const, can not change the value of the iterator; the map iterator not modify the key value, allowing to modify the value argument.

(3) map the subscript operator support, set the subscript operation is not supported. map can key do subscript, map the subscript operator [] A key as the index to perform a search, if the key is not present, then insert an element of the key and mapped_type type default value has to the map, Thus subscript operator [] caution required in the map application, const_map can not only desirable to determine the presence of a certain critical value is not desirable and should not be used when the element is inserted, mapped_type type has no default value should not be used. If you find the need to solve, as far as possible with the find.

2: STL's allocator

The STL STL package dispenser for the underlying details of memory management in the container. In C ++, which memory configuration and release as follows:

The new operator in two stages: (1) call :: operator new configuration memory; (2) configured to call the target content object constructor

delete operation in two stages: (1) function call object configuration Greek; (2) off the memory release :: operator delete employees

For precision division, STL allocator to two separate phases of operation regions: a memory arranged alloc :: allocate () is responsible, responsible for releasing the memory alloc :: deallocate (); object is constructed from :: construct () is responsible for, object destruction () is responsible :: destroy.

Meanwhile, in order to enhance the efficiency of memory management, reduce application memory small memory fragmentation problems caused, SGI STL uses a two-stage configuration, a space allocation when more than 128B, the first stage will use the space configurator; when the space allocated size is smaller than 128B, the second stage will use spatial configurator. The first stage is used as the spatial configuration malloc (), realloc (), free () function to allocate and free memory space, while the second stage uses the configuration space memory pool technology to manage memory through the free list.

3: About iterator removing elements

For sequence vessel vector, deque, the post-use erase (itertor), each element behind the iterator will fail, but behind each element will move forward one position, it will return the next valid erase iterator; 2. for each map set associative containers, the use of the erase (iterator), the current iterator element failure, but its structure is a red-black tree, delete the current element, it will not affect the iterator to the next element, so before calling erase, record iterator to the next element. 3. For the list, it is not continuous use of the memory allocation, and its erase method also returns the next valid Iterator, so the above two kinds of the correct way may be used.

4: Several underlying container implemented STL

1.vector underlying data structure is an array that supports fast random access
 
2.list underlying data structure is doubly linked list that supports rapid additions and deletions
 
3.deque underlying data structure to 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
deque is a deque (double-ended queue), it is stored in stack form content is stored as:
[Reactor 1] -> [reactor 2] -> [reactor 3] -> ...
Save several elements each stack, the stack and then the stack pointer to there between, it appears to bind the vector and list items.
 
4.stack deque or bottom generally implemented with a list, the head can be closed because no vector should be limited capacity size, expansion Processed
 
5.queue deque or bottom generally implemented with a list, the head can be closed because no vector should be limited capacity size, expansion Processed
 
(Stack and queue adapter fact, not called container, because the container is then packaged)
 
6.priority_queue underlying data structure is generally the bottom of the container vector, heap stack to process the rules governing the underlying container implementation
 
7.set underlying red-black tree data structure, ordered, will not be repeated
 
8.multiset underlying red-black tree data structure, ordered, repeatable 
 
9.map underlying red-black tree data structure, ordered, will not be repeated
 
10.multimap underlying red-black tree 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 

5: The basic components of the STL

STL has three core components: the container (Container), algorithm (Algorithms), the iterator (Iterator), container adapter (container adaptor), function object (functor), in addition to other standard STL components. Popular talk:

Container: something to hold things, a cup filled with water, put the salt water of the sea, in the classroom ...... STL containers can accommodate people is a template class of some data.

Algorithm: that is pouring into the cup, the sewage into the sea, expel people from inside the classroom ...... STL algorithms, data is processed inside the container method of operation.

Iterator: pour into the cup kettle, sewage pipes, thrust people that property managers in ...... STL iterators: traversing the data in the object container. When data is stored in the container is processed, the iterator moves from one member to another member. He can move according to a predefined order among some members of the container. Ordinary one-dimensional array, a vector, and deque list, the iterator is a pointer.

Let us look at how the experts said:

Container (container): container is a method of data organization in memory, e.g., arrays, stacks, queues, linked lists, or binary (STL but these are not standard containers). STL data structure in the container is a finite set of values ​​storing a type T (Template), the interior of the container is generally achieved class. These values ​​may be the object itself, if the data type is represented by T, then Class.

Algorithm (algorithm): algorithm is applied on the container in various ways in the processing contents of behavior or functionality. For example, sort the container contents, copy, merge and retrieval algorithms. In STL, the performance of the algorithm is a template function. These functions are not members of the class of vessel function. Instead, they are independent functions. One surprising feature of the algorithm is so versatile. STL not only be used for containers, but also for an array of conventional C ++, or any other application specified container.

Iterator (iterator): Once a container for data type and behavior (algorithm) is selected, then the only thing left for him to do is to make interaction with iterators. Generation of the device can be considered as a pointer to a common element in the container. The increment may be a pointer increment the iterator that make a point to each of the subsequent container element. An iterator is a key part of the STL algorithm as it and the container together.
6 Why use iterators instead of pointers

1, iterators

Iterator (iterative) mode, also known as the Cursor (cursor) mode, for providing a sequential access method for each element a polymeric object, without exposing the interior of the object representation. Or may be more readily understood say: Iterator mode is used in a pattern aggregate object, by using this mode, so we can know without internal object represents, in a certain order (provided by the method iterator) polymerization access each element object.

Due to the above characteristics Iterator mode: the aggregate object is coupled, to some extent, limit its widespread use, generally used only for supporting the bottom of the polymerization type, such as the STL list, vector, stack and other containers and the like ostream_iterator extension iterator.

2, the difference between the pointer and iterator

Iterator is not a pointer, a class template, acting like pointer. He just mimics some of the function pointer by overloading some operators pointer, ->, *, +, - and the like. Iterator encapsulates the pointer is a "can traverse the vessel all or part of elements of STL (Standard Template Library)" object, essentially encapsulates the original pointer, a ramp-up pointer concept (Lift), provided than to pointer senior behavior, the equivalent of an intelligent pointer, he can achieve different ++ according to different types of data structures - and other operations.

Iterator returns an object reference value is not an object, so the output iterator cout can only use the value * value can not be directly output itself.

3, causes the iterator

Iterator class way to access the different collections of abstracts access logic, such structures do not expose the inner set to achieve the effect of the loop through the collection.

7: STL in the difference resize and reserse

 

1.capacity

 

   It refers to the total number of elements in the container before the allocation of new storage space that can be stored.

 

2. size

 

   It refers to the number of elements currently stored in the container

 

 

 

After clarify these two concepts, it is easy to understand the difference between the reserve and resize

 

 

 

1) .reserve represents a container reserve space, but not really create an object, you need to create objects by insert () or push_back () and so on.

 

resize both the allocated space, but also creates an object.

 

 

 

2) .reserve capacity only modify the size, the size does not modify the size, a resize modify both capacity size, size modify size.

8: struct and class differences

In C ++, you can use struct and class definitions class can inherit. The difference is: the default permissions and inherited structural default access is public, and the class of default and default access permissions inheritance is private.

Further, a template class can define the class parameter, such as template <class T, int i>.

9 classes can be defined in reference types, but need to be initialized in the initial list

Common data type size 10

 

 11 C ++ int type several hexadecimal literals

 

 The remaining ::::::::  little attention Gets function

After a character input from the keyboard, the ENTER key will end the read operation is stored as '\ 0'. Finally, there will be a character constant hiding place to place '\ 0'. In particular, pay attention to these points. .

Strcat This function is the way from the first '\ 0' at the beginning of stitching. strlen length is not included in the count '\ 0'.

Byte alignment is very important knowledge. They are generally aligned in the length of the pointer.

0x20150810
If the storage according to the big endian mode: from higher addresses to lower addresses: 20150810
                                  Output from the low to higher addresses: 20150810
If the little-endian mode stored according to: Low to higher addresses: 10081520 
                                  Output from higher to lower addresses: 08102015
 
A brief register variables: it is stored in the variable cpu registers, when a variable requires repeated reading and writing, does not require repeated access memory, and you can use it directly, and, register keyword is a suggestion type the compiler may be because the variable does not meet certain conditions and renounce the use of register variables. Generally the local variables and automatically register variable as a function parameter.
所以当定义一个静态变量为寄存器变量,编译器不会通过,它仍然还是静态变量。简单局部变量,不说明都是auto的。
TCP连接不能自动检测断连的现象;
TCP中KEEPALIVE机制是默认不打开的,需要通过setsockopt将SOL_SOCKET.SO_KEEPALIVE设置为1则是打开,打开后会定时向连接对方发送ACK包(linux下默认是7200s 即2小时发生一次发送一次握手信息),如果在发送ACK包后对方不回应才能检测道对方的断开信息
Heartbeat机制是客户端每个一段时间向服务器发送数据包,来通知服务端该客户端保持连接
 
Quest Clear!!!!

 

 

Guess you like

Origin www.cnblogs.com/Tonarinototoro/p/11469689.html