|
-
February 14th, 2008, 01:12 PM
#1
For-loop: Vector Iterator vs. Vector.at() or []
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.
-
February 14th, 2008, 01:19 PM
#2
Re: For-loop: Vector Iterator vs. Vector.at() or []
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.
-
February 14th, 2008, 02:56 PM
#3
Re: For-loop: Vector Iterator vs. Vector.at() or []
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.
-
February 14th, 2008, 03:10 PM
#4
Re: For-loop: Vector Iterator vs. Vector.at() or []
Thank you. That elucidates me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|