Supposing I want to access a vector's data sequentially using a for-loop, should I use an iterator or can I simply access elements with at() or []?
I want to know about the advantages or disadvantages.
Printable View
Supposing I want to access a vector's data sequentially using a for-loop, should I use an iterator or can I simply access elements with at() or []?
I want to know about the advantages or disadvantages.
In the case of a vector, I'm not sure it makes much difference.
The advantage of using an iterator is that the same code would work if you later decided to swap in, say, a set instead.
1) As mentioned above, using iterators increases the ability
to write generic functions. Also, in debug mode, some
iterators have validity checks in them.
2) Operator [] tends to be more readable since the code looks
like code using simple c-style arrays.
3) at() will throw an exception if the index is out of range.
Thank you. That elucidates me.