Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

iterator invalidation

Name: Anonymous 2018-06-28 11:20

know of any real-world examples of iterator invalidation affecting mutable data structures?

Name: Anonymous 2018-06-28 12:33

Vector better than list: any aplication that requires a grid representation for example (any computer vision algorithm, for instance) in which the size of the grid is not known in advance (otherwise you would use array). In this case, you would need random access to container elements and vector is O(1) (constant time). Another example is for example when you have to build a tree (for a tree-based path planning algorithm, for instance). You do not know the number of nodes in advance, so you just push_back nodes all the time and store in their nodes the indices of the parents.

List better than vector: when you are going to do a lot of insertions/deletions in the middle of the container (note middle I mean not first/last elements). Imagine you are implementing a videogame in which many enemies are appearing and you have to kill them. Everytime you kill one, the proper thing to do is to detele that enemy so that it does not consume memory. However, it is very unlikely that you kill enemy 1. Therefore, you would need to remove that enemy from a list of enemies. If you are implementing insertion-sort-like algorithms a list can be also very helpful since you are all the time moving elements within the list.

Name: Anonymous 2018-06-28 15:24

I admit I do not recognize the jargon.

Name: Anonymous 2018-06-29 0:40

>>3
I too had to google iterator invalidation

Don't change these.
Name: Email:
Entire Thread Thread List