Get pointer to node in std::list or std::forward_list

后端 未结 5 608
猫巷女王i
猫巷女王i 2021-01-14 10:41

I am planning to use std::list in my code, I decided not to use std::forward_list, because for deletions (I figured) the whole list will have to traversed, O(N) complexity f

5条回答
  •  自闭症患者
    2021-01-14 10:59

    I am not sure if this iterator will be valid if the list is modified

    Yeah, in the general case, storing iterators is risky unless you keep a close eye on the operations performed on your container.

    Problem is, this is just the same for a pointer. In fact, for many containers, iterators are implemented as pointers.

    So either store an iterator or a pointer if you like but, either way, keep an eye on the iterator invalidation rules:

    • Iterator invalidation rules

提交回复
热议问题