The second round is about vector and deque.

Source: Internet
Author: User

Vector is the earliest STL container I used, and it is easy to use. Its structure is clear and easy to understand. deque is somewhat mysterious. One of the most notable features of deque is that it can insert members from the front-end of the container, and the efficiency is very high. I thought it was amazing and I don't know how to do it. STLSource codeIt seems so painful, so today I have not understood its structure accurately. but later I used more, and I also had some knowledge about its habits. Some of them are actually very advantageous to vector. next we will compare the two:   *. Let's talk about the internal structure. A vector is a continuous memory. This continuous memory will be re-alloc with the addition of members, and when re-allocating, the size of the allocated memory will be more than actually needed. When you add a member again, you can add it to the extra space, this does not cause the memory to be re-allocated every time a Member is added. vector encapsulates a continuous internal memory, which is my favorite place, because it can directly convert its members into pointers for access, which is flexible.   *. Deque can be randomly accessed based on an index, so I thought it had a continuous internal memory until one time I did thisProgramI realized this error when I got it. deque should be a chain table consisting of many fixed-length memory blocks. This is what I guess, because it seems that only this structure can match its performance.   *. Adding data to the vector and deque containers should be fast. After all, this is the selling point of the two containers. I have not specifically tested this.   *. The traversal speed of vector is very fast. It should be up to the limit. No matter whether you use iterator to traverse or use an incremental subscript for access, the compiler can achieve the highest efficiency through optimization.   *. Deque's traversal speed is not slow. If iterator is used to traverse, it can be similar to the vector's efficiency. However, if the traversal is directly carried out using an incremental subscript, it seems that the compiler cannot optimize to the highest efficiency, it seems to be about twice as slow: STD: deque <int> Buf; STD: deque <int >:: iterator it; Int sum = 0; For (IT = Buf. Begin (); it! = Buf. End (); It ++) // faster Traversal Sum + = * it; For (INT I = 0; I <Buf. Size (); I ++) // The traversal is slow. Sum + = Buf [I];   *. The memory allocated within the vector will never be released, even if you call clear (). This is very bad and misleading. it is possible that a vector only requires a large amount of capacity in an instant, but most of the time only requires a small amount of capacity, but the result is that it takes a lot of time, memory not used. vector does not provide a function to release its internal memory, but there is a simple method that was found on the Internet a few days ago: I _math: vector <byte> Buf; Buf. Resize (100000); // allocated a memory of at least 100000 bytes If (true) // clear the Buf memory { I _math: vector <byte> T; Buf. Swap (t); // swap the memory to a temporary vector. } Assert (BUF. Capacity () = 0); // The memory is cleared.   *. Deque is different. deque will never occupy too much redundant memory. You just need to resize it to a desired size, and it will automatically release the memory occupied by the excess.   *. Another bad thing about vector is that when you add a member to a vector, all pointers pointing to the original member of this vector cannot be guaranteed to be effective, because the vector will re-alloc memory. however, deque does not. No matter whether a new member is added from the front or back, the old member will not move the position, which is sometimes useful.   Therefore, I think deque has an advantage in many places. Compared with vector, deque lacks non-sequential memory and is not flexible enough to use, but it is more cost-effective for memory usage, the access efficiency is not much slower than that of vector. Of course, it can also be inserted and deleted quickly from the front, which is an overwhelming advantage. in short, deque is the kind that can help in key times, and it may be better for vector at ordinary times.   Finally, by the way, I don't like list at all. I have almost never used list. It is very slow to add members to list (relative to vector and deque ), it seems that every time a Member is added, the memory is allocated, and its traversal is also slow. It seems that it is faster than the map traversal and cannot be accessed randomly. the only advantage is that it can be inserted and deleted in the middle of the container, but I think it can be solved by adding some techniques to vector/deque. Anyway, I have rarely encountered a problem where list is not available in the program. Maybe I have not written many program types yet.   PS. I use stlport, and the implementation of VC should be similar.

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.