Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Iterator Invalidation and Safety | Introduction
C++ STL Containers and Algorithms

bookIterator Invalidation and Safety

Understanding how iterators become invalid is essential for writing reliable C++ code with STL containers and algorithms. Iterator invalidation occurs when an iterator that once pointed to a valid element becomes unsafe to use after the container is modified. This can cause undefined behavior, crashes, or hard-to-trace bugs.

  • Adding or removing elements from a std::vector may invalidate iterators, especially when reallocation occurs;
  • Operations like insert, erase, or resize affect iterator validity differently depending on the container;
  • Containers such as std::list often preserve iterator validity during insertions and deletions.

Iterator invalidation happens after operations that alter a container's structure or contents. The exact behavior depends on the container type and the operation performed.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> #include <vector> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; // Obtain an iterator to the second element auto it = numbers.begin() + 1; // Insert a new element at the beginning numbers.insert(numbers.begin(), 0); // 'it' may now be invalidated due to possible reallocation // Accessing *it is unsafe std::cout << "Possible invalid iterator value: "; std::cout << *it << std::endl; // Undefined behavior // Erase an element and try to use the iterator again it = numbers.begin() + 2; numbers.erase(numbers.begin() + 1); // 'it' still points to the old position, which may now be invalid std::cout << "After erase, possible invalid iterator value: "; std::cout << *it << std::endl; // Undefined behavior }
Note
Note

When using STL algorithms, be aware of operations that may invalidate iterators. Using an invalidated iterator can compromise your program's safety and correctness.

question mark

Which of the following operations is most likely to invalidate all iterators to a std::vector?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 6.67

bookIterator Invalidation and Safety

Deslize para mostrar o menu

Understanding how iterators become invalid is essential for writing reliable C++ code with STL containers and algorithms. Iterator invalidation occurs when an iterator that once pointed to a valid element becomes unsafe to use after the container is modified. This can cause undefined behavior, crashes, or hard-to-trace bugs.

  • Adding or removing elements from a std::vector may invalidate iterators, especially when reallocation occurs;
  • Operations like insert, erase, or resize affect iterator validity differently depending on the container;
  • Containers such as std::list often preserve iterator validity during insertions and deletions.

Iterator invalidation happens after operations that alter a container's structure or contents. The exact behavior depends on the container type and the operation performed.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526
#include <iostream> #include <vector> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; // Obtain an iterator to the second element auto it = numbers.begin() + 1; // Insert a new element at the beginning numbers.insert(numbers.begin(), 0); // 'it' may now be invalidated due to possible reallocation // Accessing *it is unsafe std::cout << "Possible invalid iterator value: "; std::cout << *it << std::endl; // Undefined behavior // Erase an element and try to use the iterator again it = numbers.begin() + 2; numbers.erase(numbers.begin() + 1); // 'it' still points to the old position, which may now be invalid std::cout << "After erase, possible invalid iterator value: "; std::cout << *it << std::endl; // Undefined behavior }
Note
Note

When using STL algorithms, be aware of operations that may invalidate iterators. Using an invalidated iterator can compromise your program's safety and correctness.

question mark

Which of the following operations is most likely to invalidate all iterators to a std::vector?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt