What are the common misuse of using STL containers with iterators?
Iterators are used to point at the memory addresses of STL containers. They are primarily used in sequences of numbers, characters etc. They reduce the complexity and execution time of the program.
Iterators play a critical role in connecting algorithm with containers along with the manipulation of data stored inside the containers. The most obvious form of an iterator is a pointer. A pointer can point to elements in an array and can iterate through them using the increment operator (++).
Forgetting that iterators are quite often invalidated if you change the container by inserting or erasing container members.
For many great tips on using STL I highly recommend Scott Meyers's book "Effective STL" (sanitised Amazon link)
The end range check should be using != and not < since the order of the pointers isn't guaranteed.
Example:
for(it = list.begin(); it != list.end(); ++it)
{
// do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With