myVector = {1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2}; and I decide to, "For each 1 in the vector, add an additional 2 in the vector": |
|
|
|
|
|
| Both the shuffle (obviously) and the erase invalidate iterators. |
| Your first example (push_back) is also bad. Don't do that either. Basically, if the vector changes (not the value of elements in it, but the vector), your existing iterators are no longer to be used. |
| Is there a safe way to re-order |
| Peter87 wrote: |
|---|
| Shuffle is fine. It does not invalidate any iterators. |
| Straightforward? |
if (*i == 1)| Is there a safe way to re-order/delete pieces of a vector that you're currently iterating through |
erase).
|
|
|
|
| I would think using size() would be risky as well. |
| does the compiler know when it can and when it can't optimize x.size() to a constant? |
|
|