1.c++ STL has been widely praised and used by many people, not only to provide convenient containers like vector, string, list, and more importantly, STL encapsulates a lot of complex data structure algorithms and a large number of commonly used data structure operations. Vector encapsulated Array, list encapsulates list, map and set encapsulate two fork tree, etc.
2. Standard associative container set, multiset, map, Multimap internal use is a very efficient balanced retrieval binary tree: Red black tree, also become RB tree (Red-blacktree). The statistical performance of RB trees is better than that of normal balanced binary trees.
The use of 3.STL map and set is not complex, but there are some difficult areas to understand, such as:
Map:type Pair<constkey, T>, many different const keys correspond to a set of T objects, all recordsets as long as the const key is not the same, T has nothing to do!
Set:type Const Key. Save only a single pair of const keys, no map of the T-like! Can be seen as a special case of map
(1) Why is the insertion and deletion efficiency of map and set higher than with other sequence containers? Tree
A: There is no need for memory copy and memory movement for the associated container. That's right, that's true. All elements in the map and set containers are stored as nodes, with a node structure similar to the linked list, pointing to the parent and child nodes
(2) Why does the previously saved iterator not expire after each insert?
A: Iterator here is a pointer to the node, the memory is not changed, the pointer to the memory is how to invalidate it (of course, the deleted element itself has been invalidated). Each time the pointer is deleted and inserted, it is possible for the cursor to fail relative to the vector, and the call Push_back at the end of the insertion. Because in order to ensure the continuous storage of internal data, the iterator pointed to the block within the deletion and insertion process may have been overwritten by other memory or memory has been freed. Even when the push_back, the container internal space may not be enough, need a new larger memory, only the previous memory freed, request new larger memory, the existing data elements into the new memory, and finally put the elements to be inserted into the last, then the previous memory pointer is naturally unusable. In particular, when working with algorithms such as find, keep this principle in mind: do not use outdated iterator.
(3) Why map and set cannot have a reserve function like a vector to pre-allocate data?
A: As I have said before, the rationale for this is that it is not the element itself that is stored inside the map and set, but rather the node that contains the element. That is to say, map internal use of the Alloc is not Map<key, Data, Compare, alloc> declaration of the time from the parameters of the incoming Alloc. For example:
4.set, Multiset
Set and Multiset automatically sort elements according to specific sorting criteria, the elements in set are not allowed to be duplicated, and multiset can be repeated.
Because it is ordered, the elements in the set cannot be modified, only deleted and then added.
The element types added to the set must be overloaded with the < operator for sorting. Sorting meets the following guidelines:
1, asymmetric, if the a<b is true, then B<a is false.
2, can pass, if a<b,b<c, then a<c.
3, A<a Forever is false.
The set determines whether the elements are equal:
if (! ( A<b | | B<a)), when both A<b and B<a are false, they are equal.
5.map,multimap
Map and Multimap The pair of key and value as an element, automatically sorts the elements according to the ordering criteria of key, the key of the elements in the map does not allow repetition, multimap can be repeated.
Map<key,value>
Because it is sort, the key of the element in the map cannot be modified, but can only be deleted and then added. The value corresponding to key can be modified.
The key type of the element added to the map must be overloaded with the < operator for sorting. The sort is consistent with the set rule.
6. Function method of List
There are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not for the fast random access design, but has a more general method.
List: Order is the most important feature of the list: it ensures that the elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (this is recommended for linkedlist use only. A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and remove elements from the middle of a list.
ArrayList: A list implemented by an array. Allows fast random access to elements, but inserts and removes elements in the middle of the list is slow. Listiterator should only be used to traverse the ArrayList backward, not to insert and remove elements. Because that's much more expensive than linkedlist.
LinkedList: Sequential access is optimized, and the cost of inserting and deleting to the list is small. Random access is relatively slow. (use ArrayList instead.) ) also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), which are not defined in any interface or base class Enables LinkedList to be used as stacks, queues, and bidirectional queues
What is the difference between 7..1 hash_map and map?
constructor function. Hash_map requires a hash function, equals a function, and a map requires only a comparison function (less than a function).
Storage structure. Hash_map use hash table storage, map is generally used red-black tree (RB tree) to achieve. So its memory data structure is not the same.
7.2 When do I need to use hash_map and when do I need a map?
Overall, the Hash_map lookup speed is faster than map, and the lookup speed is basic and the data data size is the constant level, and the map lookup speed is the log (n) level. Not necessarily the constant is smaller than log (n), hash and hash function time-consuming, understand, if you consider efficiency, especially when the element reaches a certain order of magnitude, consider hash_map. But if you are particularly strict about memory usage, and you want the program to consume as little memory as possible, be careful, hash_map may embarrass you, especially if your hash_map object is so special, you're more out of control, and Hash_map's construction is slow.
Do you know how to choose now? Weigh three factors: find speed, amount of data, memory usage.
8. Some recommendations for use:
Level 1-use as map only: static array
Level 2-Saves fixed-length data, which is also all-in-one: dynamic arrays (static arrays are OK if the length is fixed at the beginning)
Level 3-the ability to save variable-length arrays, which requires a dynamic increase, focuses on the speed of finding data: using vectors
Level 3-the ability to save variable-length arrays, which requires a dynamic increase, focuses on increasing the speed of deleting data: Using the list
Level 4-complex operation of data, i.e. the ability to delete data before and after, and good data access speed: Using Deque
Level 5-The number of additions and deletions in the data center is more: Using list, it is suggested that on the basis of sorting, the bulk of the additions and deletions can provide the greatest guarantee for the operation efficiency.
Level 6-none of the above can be found: Combine STL containers or create special data structures to implement
9.
(1). Vector-an array that will automatically grow
Vector<int>vec (10)//a container with 10 int elements
Vector<float> VEC (10, 0.5)//a container with 10 float elements, and they have a value of 0.5
Vector<int>::iterator ITER;
for (iter = Vec.begin (); ITER! = Vec.end (); iter++)
{
//. . . . . . .
}
Vector because the array grows only forward, so it only provides back-end inserts and back-end deletions,
That is push_back and pop_back. Of course, in the front and center to manipulate the data is also possible,
With insert and erase, but the front end and the middle of the data operation will inevitably cause the data block movement,
This has a very large impact on performance.
The biggest advantage is the ability to randomly access.
Vector<t1>::iterator related methods are:
Begin (): Used to obtain a pointer to the first element of a vector
End (): Used to get a pointer to the position after the last element of the vector, note that it is not pointing to the last element.
Erase (Vector<t1>::iterator): Used to delete the element pointed to by the iterator that was passed in as a parameter.
(2). List-good at inserting and deleting linked lists
list<string>milkshakes; List<int> Scores;
Push_back, Pop_backpush_front. Pop_front
List is an implementation of a doubly linked list.
To provide two-way traversal capability, the list is two more pointers to the front and back than the average data unit.
An example of using iterator to delete an element
List<string> stringlist;
List<string>::iterator ITER;
Advance (ITER, 5); Point iterator to the fifth element of Stringlist
Stringlist.erase (iterator);//delete
So after the delete operation, where does the iterator of the incoming erase () method point to? It points to the next element of the element pointed to before the delete operation.
(3). Deque-double-ended queue with advantages of both vector and list
(4). Summary comparison and general usage guidelines for these three templates
These three templates are part of the sequence class template, and you can see that they have a common approach
Size (): Get container size
Begin (): Gets a pointer to the first element within the container (the type of the pointer varies depending on the container)
End (): Gets a pointer to a position after the last element within the container
Erase (): Delete the element that the incoming pointer points to
Clear (): Clears all elements
= = Operator: Determines whether two containers are equal
= operator: Used to assign a value to a container
The above three templates have their own characteristics
The data of the vector template is continuously arranged in memory, so the speed of the random access element (that is, accessed through the [] operator) is the fastest, and the array is consistent. Also because it is continuously arranged, it is slow to delete or add elements at a location other than the tail, so avoid this when using vectors.
The data of the list template is chained, so the element cannot be accessed randomly. Its advantage is that the speed at which the elements are deleted is added at any location.
Deque templates are implemented by linking several pieces of continuous data, thus balancing the characteristics of the above two containers
C + + interview notes--stl templates and containers