473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Iterator

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

//Tony
Jul 23 '05 #1
9 1512
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach wrote:
ArrayList is not a standard class


It is - in Java :-)

Jul 23 '05 #3
Tony Johansson wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4
Rapscallion wrote:
Alf P. Steinbach wrote:
ArrayList is not a standard class


It is - in Java :-)


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

Jul 23 '05 #5
Rolf Magnus wrote:
Rapscallion wrote:

Alf P. Steinbach wrote:
ArrayList is not a standard class


It is - in Java :-)

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/...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.

Jul 23 '05 #6
Andrew McDonagh wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7
Ioannis Vranos wrote:
Andrew McDonagh wrote:
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++ ?

Jul 23 '05 #8
Andrew McDonagh wrote:
So, what has it to do with C++?

Absolutely nothing, I was just answering Rolf's question.

My mistake, I thought you were the original poster. :-)

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #9
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:Y1*********************@newsc.telia.net...
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[i] = 2.2;

you would have to change them all.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.