Is end() required to be constant in an STL map/set?

前端 未结 10 502
别跟我提以往
别跟我提以往 2021-02-01 15:00

§23.1.2.8 in the standard states that insertion/deletion operations on a set/map will not invalidate any iterators to those objects (except iterators pointing to a deleted eleme

10条回答
  •  醉梦人生
    2021-02-01 15:26

    I think it is clear:

    • end() returns an iterator to the element one passed the end.

    • Insertion/Deletion do not affect existing iterators so the returned values is always valid (unless you try to delete the element one passed then end (but that would result in undefined behavior anyway).

    • Thus any new iterator generated by end() (would be different but) when compared with the original using operator== would return true.

    • Also any intermediate values generated using the assignment operator= have a post condition that they compare equal with operator== and operator== is transitive for iterators.

    So yes it is valid to store the iterator returned by end() (but only because of the guarantees with associative containers, therefor it would not be valid for vector etc).

    Remember the iterator is not necessarily a pointer. It can potentially be an object where the designer of the container has defined all the operations on the class.

提交回复
热议问题