Balance between vector, map, and list

Source: Internet
Author: User
Balance between vector, map, and list

From: [http://www.codingnow.com/2004/board/view.php? Paster= 776]

I want to create a data structure to store data in an orderly manner for convenient search, and the efficiency of data insertion is not too low.

When the data is almost unchanged, vector is the best solution. We only need to put the data in. After sort, it will be okay to use binary_search.

If data is often inserted, we have to consider using list to achieve the fastest insertion speed, but it is not conducive to retrieval.

Map is a lotProgramMember favorites, STL map is implemented using RB tree. It has a fast insert speed and a search speed comparable to that of binary search. However, it requires a lot of additional information, three pointers and one color value. In addition, the insert efficiency of map is another concern.

Here, Yunfeng proposes a Data Structure for reference only:

I want to use a scattered array to store ordered data. Physical online data can be searched in binary mode, however, some discrete properties can prevent the insert operation from moving a large amount of data. The data I want to see is saved in the memory in the following status:

X... xx... X...

We regard X as data, and "." as redundant information. No matter the actual data or redundant data, the linear distribution is in the memory. We can implement it using vector. Of course, we need to use 1 bit to distinguish the data type. This bit does not need to be recorded separately. In most cases, the data itself can be borrowed. For example, if a word in a data node is a valid pointer, we can use a null pointer to indicate that the node is a redundant node.

Then we place an offset in the redundant node to associate the discrete node:

The offset between X... X and x 4-2-3-4 X indicates the position of the data node closest to the node. With this information, we can easily perform binary search.

As for inserting a node, if there is a redundant node, we can replace it. If not, we need to move forward or backward, but the movement is generally smaller than the intensive array.

DetailedAlgorithmI will not give it any more. This article only records the sudden inspiration.

Without careful consideration of this algorithm, the right should be extracted and considered again in the future if it is feasible!

Related Article

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.