STD: where will the insert of set put the new element?

Source: Internet
Author: User

In fact, I want to put it first, so that I can use begin to access this new element-I am working on a connection pool and want to put the connection I just used up at the beginning, in this way, it can be used frequently, and those that are not commonly used will be discarded and deleted gradually. SlaveCodeAccording to the actual running situation, it is the pattern I want, but is it accidental?

 

After finding this post, STD: Set uses a tree instead of an array to store elements. In this case, the so-called front and back are meaningless! This is something you should have imagined! The STL set encapsulates the internal storage space andAlgorithmAs long as you use begin, next can traverse all elements without having to worry about its internal storage location. Of course, STD: Set still has a fixed "storage location". That is to say, if other elements do not change, extract the elements at the begin location (erase ), put it back (insert) and it will still be in the begin position -- here we will talk about the enumeration order.

 

In this case, my previous practice is correct. However, this is probably because of luck. Because there is no guarantee that the name of the insert function will be placed in the begin position. In addition, it is very likely that another set type is used. For example, changing set to list will not achieve the expected results. Therefore, this piece of code has a hidden risk. After a long time, people may forget this issue and replace set: insert with another -- a bug is very likely to occur.

 

Therefore, the Code still cannot be used with a small trick. It is better to clearly understand the intention. So what set object can explicitly specify the insert position? Vector: insert?

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.