Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iterators

Status
Not open for further replies.

chineerat

Technical User
Oct 12, 2003
42
0
0
TT
i have 2 for loop and a list
one is

for ( list<numbers>::const_iterator i= poly1.begin(); i != poly1.end(); i++){
other is
for ( list<numbers>::const_iterator j= poly1.begin(); j != poly1.end(); j++ )

my problem is want iterator j to start on the element after i eg
i am doing
for ( list<numbers>::const_iterator j= poly1.begin() +1 ; j != poly1.end(); j++ )

but it doesn't work!
anyideas????
THANKS
 
Does it work if you do

list<numbers>::const_iterator j= poly1.begin();
j++;
for (j ; j != poly1.end(); j++ )

I think i tis because you are trying to treat your list as an iterator in it's own right, where as in fact a list (and this is all guesswork) in reality is a list of pointers to your data, and so may not be concurrent in memory, so you can't just list.begin()+1

But if anyone can explain this better, or correct me please do !


K

 
If you are doing a nested loop, and want j to start at i + 1 every time, you should write something like this :

list<numbers>::const_iterator i;
list<numbers>::const_iterator j;

for (i = poly1.begin(); i != poly1.end(); i++){
for (j = i+1; j != poly1.end(); j++ ) {
...
}
}

/JOlesen
 
chineerat,

You still don't want to use a map for your polynomial calculation purposes? I tried to code a class using map and another using lists. I think using a map is really easier than 2 lists. Maybe you are obliged to use lists.
Otherwise, may I advise you to re-take a look at thread :
thread207-711049

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top