STL Reading Notes

Source: Internet
Author: User

Vector-an array that will automatically grow

Vectors are also known as vector arrays, and he is to solve the definition of an array in a program that is
Can not dynamically change the size of this disadvantage occurs.
The general program implementation is to create a fixed-length array at the same time as the class is created.
As the data is constantly being written, once the array is filled, a larger memory area is re-opened,
Copy the original data to the new memory area, discard the original memory, so repeatedly.
Because the program automatically manages the growth of arrays, it's really a lot easier for us programmers,
Just plug the data into it, and of course the physical memory and the virtual memory are blown out.
It's the operating system to get you in trouble.:-)
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.
For all arrays, the greatest advantage is the ability to randomly access.
In Vector, both the at and [] operators are provided for random access.
Because each data is of the same size and is arranged in memory at no interval,
So in order to operate on a certain data, it is only possible to use an expression to calculate the address directly:
Address = base + Index * datasize
In the same way, the memory of the vector is opened up, initialized, clear, it does not need to spend much effort,
There is only one piece of memory from beginning to end.

List-good at inserting and deleting linked lists

There will be black and white, and all things in the world will appear in pairs.
A linked list is the opposite of an array.
The array itself is not dynamically capable of growth (the program must also re-open memory to implement),
And the list of powerful is the dynamic growth and deletion of the ability.
But for the random access ability of arrays, the list is weak.
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.
This is not the way, after all, now the PC memory structure is a large array,
It takes more space for a list to implement its own functionality in a different environment.
List provides four methods of Push_back,push_front,pop_back,pop_front
To make it easier to manipulate the data additions and deletions at both ends of the list, but less the vector's at and [] operators
Method of randomly accessing data. Not impossible, but a list designer.
And don't want list to do those things because they're going to do a really bad job.
For list, clearing all the elements inside a container is a hard work,
Because the memory of all data units is not contiguous, the list is deleted only one at a time.

deque-double-ended queue with advantages of both vector and list

Black and white, in between these two extremes is a delightful color.
Deque, as a combination of vectors and lists, does have an extraordinary strength.
The implementation of STL's deque has not been seen, but according to my own speculation,
It should be to fragment the array and add pointers to the segmented array to connect all the segments together.
Eventually becomes a large array.
Deque, like List, provides the Push_back,push_front,
Pop_back,pop_front four methods. As you can imagine, if you want to operate on both ends of the deque,
That is, to reallocate the memory area to the fixed-length array of the first and last paragraphs,
Because of the small array of segments, the cost of redistribution is not significant.
Deque, like vectors, provides methods for the at and [] operators.
To calculate the address of a certain data, though more troublesome than vector,
But the efficiency is much higher than the list.
First, the same as the list of traversal, each time the traversal of the cumulative size of each array,
When traversing to a segment, and BaseN <= Index < BaseN + Basen_length,
Addresses can be calculated by address = BaseN + Basen_index
Because the length of the post-linked list is not too long, the traversal is
The impact of overall performance is negligible.
It looks like deque is invincible, but Deque is like Agyris of Greek mythology,
Again how strong also have their own weaknesses, after the test data can be seen.

STL Reading Notes

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.