[C + +] STL related face question

Source: Internet
Author: User
Tags add time

(1) Why is the insertion and deletion efficiency of map and set higher than with other sequence containers?

Because the internal data structure of map and set is a red-black tree, its insertions and deletions do not need to be copied and moved in memory. (the insertion and deletion of red and black trees is log (n)).

(2) Why does the previously saved iterator not expire after each insert?

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.

(3) Add n elements to a vector, and the average added performance is?

Look at the output of the following code:

Vector<int> v;

for (int i=0;i<100;i++}

{

cout << v.capacity () << Endl;

V.push_back (i);

}

Can see the change of space is 0,1,2,4,8,16 ... (under GCC is so, VC is not ...)

This needs to take into account that when space is not enough, you need to open up new memory and the copy of the element will occur. In general, the space opened up is twice times the original. Then the average add time is: (1+2+4+...+ (N-LOGN))/n, that is, there are logn elements to be added when the overall shift N times, the other operating cost is 1, the average is still O (N).

(4) How to understand the container adapter, which is different from the sequential container?

Adapter intended to be a receptacle, adapter, the meaning of the coupling. Now I need a stack structure, we can use the deque to simulate, only one end of the element insertion and ejection, the other end does not move. but Deuqe, after all, cannot be directly a stack, and it does not directly meet your requirements , because you cannot prevent others from moving your things on the other end of the way. You need to wrap it up and make some restrictions so that it can only be inserted and deleted at one end. That means you have to provide a "socket", the "socket" on the deque, the other end in your program, you can use the stack structure. And the stack is the "socket" that connects the Deque and your program. On the surface you are using a stack, in fact you are using the "socket" of the stack to use Deque (because the stack is completely implemented with deque, it does not have anything else, it is just a layer of packaging on the deque, the equivalent of a "socket" function). Therefore, classes such as stack, queue, and priority_queue are generally referred to as container adapters, and they are simply adaptations of the basic container type (vector,dequeue,list).
In fact, this is also the basic idea of the adapter pattern: To convert a class's interface into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. In other words, a "socket" class is provided on the interface of a class to make it the interface you want to use.

[C + +] STL related face question

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.