iterators vs []

i dont see how iterators and [] operator differ. I am using them to cycle through a vector, and both seem to work. Which way is better, and how are they different.
[164 byte] By [bovinedragon] at [2007-11-20 10:55:52]
# 1 Re: iterators vs []
Performance wise, when you loop through all element of a list/vector using indexing, every iteration of the loop starts a element 0 and goes to i, while if you use iterators you only do a +1 operation every iteration. Depending on the size of the list/vector and implementation of the indexing operator, this can really make a diffrence.

+ there are other advantages: read http://en.wikipedia.org/wiki/Iterator#Contrasting_with_indexing
ejac at 2007-11-9 1:24:52 >
# 2 Re: iterators vs []
Iterators are a more general concept which makes them more suitable for generic algorithms. They are also more efficient to use than operator[] for certain containers, such as linked lists.

Say you have a basic singly linked list with five elements in it, and you access each element using the subscript operator. To access the Nth element with a subscript, the container must re-iterate N times through its nodes... so you'd have a total of 0 + 1 + 2 + 3 + 4 = 10 iterations in order to read only five elements. With an iterator, however, you can keep your place so that only four iterations are necessary. With thousands of elements in the list, the difference can be tremendous.
Hermit at 2007-11-9 1:25:48 >
# 3 Re: iterators vs []
thx for the posts. So the only difference is the way that these two thing perform the same operation? I guess I will try to only use iterators now, thx
bovinedragon at 2007-11-9 1:26:51 >
# 4 Re: iterators vs []
std::list doesn't support operator[]. Only containers that support random-access iterators also support operator[]. For these, there's no general way to know whether using an iterator or using operator[] is more efficient. It depends on the implementation, the CPU instruction set available and the optimizations done by the compiler.
Iterators are necessary if you're going to use STL algorithms, because those take iterators (for generality) and don't use operator[].
googler at 2007-11-9 1:27:45 >
# 5 Re: iterators vs []
For these, there's no general way to know whether using an iterator or using operator[] is more efficient. It depends on the implementation, the CPU instruction set available and the optimizations done by the compiler.Even though you are right that it is impossible to make a general statement about performance that holds true under all conditions, I would still consider it safe to assume that iterators are at least as efficient as operator[] when iterating through an STL container. I just find it hard to imagine, why any implementation would make iterators deliberately perform worse. If there is no performance gain possible, iterators can just be implemented as an integer i where dereferencing then calls operator[].
treuss at 2007-11-9 1:28:49 >
# 6 Re: iterators vs []
I would still consider it safe to assume that iterators are at least as efficient as operator[] when iterating through an STL container.That's true if all you're doing is sequential access to the container, like scaning it from begin() to end(). If you need to do true random-access (like binary search), you generally need to do index arithmetic of some kind and then the use of iterators becomes an annoying distraction.
For vector specifically, using an index to do sequential iteration is probably going to be the same speed or negligibly slower than an iterator, so the efficiency consideration should not be the deciding factor.
googler at 2007-11-9 1:29:51 >