After reviewing for some time, here is my understanding.
1. Standard sequence containers
For containers with a normal sequence, insert takes an iterator (which I will call itl ) as a parameter and inserts the elements before itl . It then returns an iterator ( itf ) that points to the first element inserted. Now you have 2 iterators, separately designating the range (first and next) of the inserted elements.
elem-elem-inserted-inserted-inserted-elem-elem | | (itf) (itl)
However, the insert operation may invalidate the itl , so we need to consider the following two situations:
1.1 Related structure
For a linked structure, itl remains valid. Therefore ( itf , itl ) forms a valid range for inserted elements.
1.2 Continuous structure
For an adjacent structure, itl not valid after insertion. However, such structures usually support random access, so it is relatively easy to get itl through simple arithmetic on itf . On the other hand, returning the iterator to the first inserted element maintains consistency.
2. The forecast list
insert_after for forward-list takes an iterator ( itf ) as a parameter and inserts the elements after it. Then it returns the iterator to the last inserted element ( itl ). Since itf remains valid, we again have a similar range.
elem-elem-inserted-inserted-inserted-elem-elem | | (itf) (itl)
3. Conclusion
Regardless of whether the insert operation returns an iterator to the first inserted element or the last inserted element, it always tries to provide access to the beginning and end of the inserted elements at the same time, which provides maximum flexibility.
Zhe chen
source share