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?