diff --git a/Chapter 5/Issues-of-Iterator-Invalidation.md b/Chapter 5/Issues-of-Iterator-Invalidation.md index f70451c..725db12 100644 --- a/Chapter 5/Issues-of-Iterator-Invalidation.md +++ b/Chapter 5/Issues-of-Iterator-Invalidation.md @@ -8,7 +8,7 @@ For example, in the following case, we use an iterator to delete all the even nu int main() { vector v; for (int i = 0; i < 20; ++i) { - vec.push_back(rand() % 100 + 1); + v.push_back(rand() % 100 + 1); } for (auto it = v.begin(); it != v.end(); ++it) { if (*it % 2 == 0) { @@ -27,7 +27,7 @@ The second example is to add *n - 1* before every even number n: int main() { vector v; for (int i = 0; i < 20; ++i) { - vec.push_back(rand() % 100 + 1); + v.push_back(rand() % 100 + 1); } for (auto it = v.begin(); it != v.end(); ++it) { if (*it % 2 == 0) { @@ -48,7 +48,7 @@ After an element is erased from the container, the elements after it will move f int main() { vector v; for (int i = 0; i < 20; ++i) { - vec.push_back(rand() % 100 + 1); + v.push_back(rand() % 100 + 1); } for (auto it = v.begin(); it != v.end();) { if (*it % 2 == 0) { @@ -67,11 +67,11 @@ After an element is erased from the container, the elements after it will move b int main() { vector v; for (int i = 0; i < 20; ++i) { - vec.push_back(rand() % 100 + 1); + v.push_back(rand() % 100 + 1); } - for (auto it = v.begin(); it != v.end();) { + for (auto it = v.begin(); it != v.end();++it) { if (*it % 2 == 0) { - v.insert(it, *it-1); + it = v.insert(it, *it-1); ++it; } }