473,403 Members | 2,323 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,403 software developers and data experts.

Iterator vs Loop

what is the difference between iterator and lops
Sep 12 '07 #1
2 10169
Nepomuk
3,112 Expert 2GB
what is the difference between iterator and lops
An Iterator is an Object, which goes through a Collection (e.g. Vector, List, ...) and you can do something with whatever the Iterator is pointing at.

A Loop is a construct, that repeats something for a certain amount of times.

Iterators and Loops are often used together. What I guess you mean is: What's the difference between
Expand|Select|Wrap|Line Numbers
  1. Vector<String> vector = new Vector<String>();
  2. vector.add("Hello");
  3. vector.add("World");
  4.  
  5. for(int i=0; i<vector.size();i++)
  6. {
  7.     System.out.println(vector.elementAt(i));
  8. }
  9.  
and
Expand|Select|Wrap|Line Numbers
  1. Vector<String> vector = new Vector<String>();
  2. vector.add("Hello");
  3. vector.add("World");
  4.  
  5. Iterator vectorIterator = vector.iterator();
  6. while(vectorIterator.hasNext())
  7. {
  8.    System.out.println(vectorIterator.next());
  9. }
  10.  
(You can replace the Vector with another Collection.)

One big advantage of the first option is, that you always know, at which position you are.

One big advantage of the second option is, that you don't have to know the size of your Collection.

I can't think of any further differences in the usage at the moment, but the concept is, as you see, quite different.

Greetings,
Nepomuk
Sep 12 '07 #2
r035198x
13,262 8TB
what is the difference between iterator and lops
If "lops" was supposed to be loop, then
an Iterator is an interface that facilitates enumeration over a collection while a loop is a program construct that facilitates repetition in a program.

P.S Changed thread title
Sep 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
6
by: samuel.y.l.cheung | last post by:
Hi, I am trying to convert a Java iterator loop to C++ STL? the loop looks like this: public static boolean func (List aList) { int minX = 0 for (Iterator iter = aList.listIterator(1);...
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
0
by: sstan | last post by:
I'm trying to implement some kind of locking and am having problem with iterating through a map that contains an object with a set. I can of course change my structure and to aviod the problem, but...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.