Four questions about map and set in STL?

Source: Internet
Author: User

the use of STL map and set is not complicated, but there are some difficult to understand, such as: # Why is the efficiency of the insertion and deletion of map and set higher than the other sequence container? # Why did the previously saved iterator not expire after each insert? # Why don't map and set have a reserve function like a vector to pre-allocate data? # When data elements increase (10000 to 20,000 comparisons), how does the insertion and search speed of map and set change? Maybe some people can answer the approximate reason, but to understand thoroughly, but also need to understand the STL's underlying data structure. C+ + STL is widely praised, also used by many people, not only to provide like vector,string, list and other convenient containers, more importantly, STL encapsulates a number of complex data structure algorithms and a large number of commonly used data structure operations. Vector package Array, list encapsulates the linked list, map and set encapsulated two fork tree, in the encapsulation of these data structures, STL in accordance with the use of the programmer's habits, as a member function of the common operations, such as: Insert, sort, delete, find and so on. Let the user in the STL use process, do not feel unfamiliar. C++STL's standard associative container set, multiset, map, Multimap Internally, is a very efficient balanced retrieval binary tree: The red-black tree, also becomes the RB tree (red-black Tree). The statistical performance of the RB tree is better than that of the general balanced Binary tree (some books are based on the author's name, adelson-Velskii and Landis, called the AVL-tree), so the STL is chosen as the internal structure of the associative container. This article does not describe the implementation of the detailed AVL tree and the RB tree, and their merits and demerits, the detailed implementation of the RB tree see red and black Trees: Theory and implementation (theory). This paper gives a brief introduction to the underlying data structures of map and set for the answers to several questions that have been raised. Why is the insertion and deletion efficiency of map and set higher than with other sequence containers? Most people say it's simple because there's no need for memory copy and memory movement for associative containers. That's right, that's true. All elements within the map and set containers are stored as nodes, with a node structure that is similar to the linked list, pointing to the parent node and child nodes. The structure chart may be as follows: A/B C/ \ /D E F g so when inserting only need to do a little transformation, the node pointer to the new node. Delete the same time, a little change after the pointer to the deletion of the node point to the other node is OK. All that is done here is that the pointer is swapped out, and the memory movement is not related. Why does the previously saved iterator not expire after each insert? Seeing the explanation of the above answer, you should already be able to explain the problem easily. Iterator here is the equivalent of a pointer to a node, the memory does not change, 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, copy the existing data elements to the new memory, and finally put the elements need 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. Why can't map and set have a reserve function like a vector to pre-allocate data? 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, the alloc used inside map is not a map.<key, Data, Compare, alloc>declared when the Alloc is passed in from the parameter. Example: Map<int,int, less<int, alloc<int> >Intmap; the allocator used in Intmap is not alloc<int>, but through the conversion of the alloc, the specific conversion method is internally passed through the Alloc<int>:: rebind redefined the new node allocator, detailed implementation see thoroughly learn the allocator in STL. In fact, you will remember that in the map and set inside the allocator has changed, the reserve method you do not expect. How does the insertion and search speed of map and set change when the number of data elements increases (10000 and 20,000 comparisons)? If you know log2 's relationship, you should have a thorough understanding of the answer. Finding in map and set is a binary lookup, that is, if there are 16 elements, you will need to compare 4 times to find the result, 32 elements, and a maximum of 5 times. So there are 10,000 of them? The maximum number of comparisons is log10000, up to 14, and 20,000 if it is a single element. Up to 15 times. See, when the amount of data increases by one time, the number of searches is only 1 more times, more than 1/14 of the search time. Once you understand this, you can safely put the elements inside. Finally, for both the map and set winter, they are compared to the efficiency of a C-language packaging library. In many UNIX and Linux platforms, there is a library called ISC, which provides a function similar to the following declaration:voidTree_init (void**tree);void*tree_srch (void**tree,int(*compare) (),void*data);voidTree_add (void**tree,int(*compare) (),void*data,void(*Del_uar) ());intTree_delete (void**tree,int(*compare) (),void*data,void(*Del_uar) ());intTree_trav (void**tree,int(*Trav_uar) ());voidTree_mung (void**tree,void(*Del_uar) ()); many people think that using these functions directly is faster than the STL map, because many templates are used in the STL map. In fact, the difference is not in the algorithm, but in the memory fragmentation. If you use these functions directly, you need to go from the MA to new nodes, when the node is particularly large, and frequent deletion and insertion, the memory fragmentation will exist, and the STL uses its own allocator to allocate memory, memory pool to manage the memory, will greatly reduce memory fragmentation, This will improve the overall performance of the system. Winter in his own system has been tested, all the former directly with the ISC function code to replace the map, the program speed is basically the same. When the time has elapsed for a long time (such as a background service program), the advantages of map will be reflected. On the other hand, using map can greatly reduce your coding difficulty while increasing the readability of your program. Why not
http://blog.csdn.net/wu_lai_314/article/details/8440655

Four questions about map and set in STL?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.