Iterator

T

Tony Johansson

Hello!


Why should an Iterator be used instead of using index when going through a
collection for example ArrayList

//Tony
 
A

Alf P. Steinbach

* Tony Johansson:
Why should an Iterator be used instead of using index when going through a
collection for example ArrayList

ArrayList is not a standard class, so no answer is possible.
 
I

Ioannis Vranos

Tony said:
Hello!


Why should an Iterator be used instead of using index when going through a
collection for example ArrayList


When the container contents can be accessed efficiently with indices, then indices are
provided. Consider vector, valarray, bitset, etc.
 
A

Andrew McDonagh

Rolf said:
Rapscallion wrote:




Just out of interest: What is it? An array or a list?

List is the interface (i.e. Java's pure abstract class 'like' construct)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html


An ArrayList is an implementation of the List interface which is backed
by an array - so 'get(int position)' maps nicely to 'array[position]'

There's also LinkedList - which implements the List interface using a
doubly linked list.
 
I

Ioannis Vranos

Andrew said:
An ArrayList is an implementation of the List interface which is backed
by an array - so 'get(int position)' maps nicely to 'array[position]'

There's also LinkedList - which implements the List interface using a
doubly linked list.


So, what has it to do with C++? C++ vector provides both random access iterators and
indices. Under .NET, in VS 2005, a managed vector version inheriting from .NET IList<T>
interface will also be provided.
 
A

Andrew McDonagh

Ioannis said:
Andrew said:
An ArrayList is an implementation of the List interface which is
backed by an array - so 'get(int position)' maps nicely to
'array[position]'

There's also LinkedList - which implements the List interface using a
doubly linked list.



So, what has it to do with C++?

Absolutely nothing, I was just answering Rolf's question.
C++ vector provides both random access
iterators and indices.

Great aren't they.

Under .NET, in VS 2005, a managed vector version
inheriting from .NET IList<T> interface will also be provided.

What's this got to do with standard C++ ?
 
C

Cy Edmunds

Tony Johansson said:
Hello!


Why should an Iterator be used instead of using index when going through a
collection for example ArrayList

//Tony

There may or not be a speed advantage, but there is almost certainly a
maintainability advantage if your algorithms don't actually need
subscripting. Consider:

#include <vector>
typedef std::vector<double> t_coll;
t_coll c;
....
for (t_coll::iterator i = c.begin(); i != c.end(); ++i)
*i = 2.2;

Now let's say that after some new requirements come in you find that you
must frequently delete items from the middle of your collection. You need to
use a list instead of a vector for efficient performance. No problem:

#include <list> // change this
typedef std::list<double> t_coll; // and this
t_coll c; // same
....
for (t_coll::iterator i = c.begin(); i != c.end(); ++i) // same
*i = 2.2; // same

Now if you had written you loops as

for (unsigned i = 0; i < c.size(); ++i)
c = 2.2;

you would have to change them all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help in hangman game 1
problem with iterator (map iterator) 3
Vector iterator problem 3
returning an iterator 'concept' 4
Tasks 1
TF-IDF 2
Iterator or for loop? 16
How to sort a CSV file with merge sort JAVA 7

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top