infinite stl list loop

B

boguz

Dear all,

I am just using a list<int> iterator in a for loop to get the all
values in the list, like this:

for (list<int>::iterator
itt=labelLists[labels3X3cube[*iter]-1].begin();
itt!=labelLists[labels3X3cube[*iter]-1].end(); itt++ )
{

std::cout << labelLists[labels3X3cube[*iter]-1].size() <<
"___"<< (*itt) << "___" <<
*(labelLists[labels3X3cube[*iter]-1].end())<< std::endl;
si10 = labelLists[labels3X3cube[*iter]-1].size();
labels3X3cube[*itt] = currentLabel;

}


and write the size of the list __ present value ___ and the end value
of the list.
The values are like this:

4___26___0
4___25___0
3___23___6316800
3___17___6316800
3___0___6316800
3___26___6316800
3___25___6316800
3___23___6316800
3___17___6316800
3___0___6316800
3___26___6316800
3___25___6316800
3___23___6316800
3___17___6316800
3___0___6316800
....

and it repeats this pattern infinitely.

In the forloop i do not make any change in the list but something
happens. Can anyone help me in solving this problem.
Thanks in advance

burak
 
B

boguz

I forgot to tell you the labelLists structure.
it is defined as

vector< list< int > > labelLists;

burak
 
O

Ondra Holub

Dear all,

I am just using a list<int> iterator in a for loop to get the all
values in the list, like this:

for (list<int>::iterator
itt=labelLists[labels3X3cube[*iter]-1].begin();
itt!=labelLists[labels3X3cube[*iter]-1].end(); itt++ )
{

std::cout << labelLists[labels3X3cube[*iter]-1].size() <<
"___"<< (*itt) << "___" <<
*(labelLists[labels3X3cube[*iter]-1].end())<< std::endl;
si10 = labelLists[labels3X3cube[*iter]-1].size();
labels3X3cube[*itt] = currentLabel;

}

and write the size of the list __ present value ___ and the end value
of the list.
The values are like this:

4___26___0
4___25___0
3___23___6316800
3___17___6316800
3___0___6316800
3___26___6316800
3___25___6316800
3___23___6316800
3___17___6316800
3___0___6316800
3___26___6316800
3___25___6316800
3___23___6316800
3___17___6316800
3___0___6316800
...

and it repeats this pattern infinitely.

In the forloop i do not make any change in the list but something
happens. Can anyone help me in solving this problem.
Thanks in advance

burak

Hi.

- If you are not changing anything, use const_iterator for walking
through container.
- I do not like much line labels3X3cube[*itt] = currentLabel; Are you
sure it is correct? I think it may change something.
- When you are incrementing iterator, use prefix notation ++it instead
of it++, it may be more effective and you will save some work to
compiler

*(labelLists[labels3X3cube[*iter]-1].end())

- Dereferencing end() is not a good idea - it is iterator pointing
beyond last valid element

Ondrej
 
T

tony_in_da_uk

for (list<int>::iterator
itt=labelLists[labels3X3cube[*iter]-1].begin();
itt!=labelLists[labels3X3cube[*iter]-1].end(); itt++ )
{
std::cout << labelLists[labels3X3cube[*iter]-1].size() <<
"___"<< (*itt) << "___" <<
*(labelLists[labels3X3cube[*iter]-1].end())<< std::endl;
si10 = labelLists[labels3X3cube[*iter]-1].size();
labels3X3cube[*itt] = currentLabel;

}

and it repeats this pattern infinitely.

burak

To sort these kind of things out, you've can work things from either
(or needs be both) ends: gradually simplify until things start
working, or take something absolutely simple (a test program iterating
over a list) and make it gradually more complex (like your broken
code), until you see what breaks it too. It's a skill and discipline
you'll need to write any significant software.

Here, the easiest and most pressing simplification will also help you
understand the problem (and fix it):

list<int>& ints = labelLists[labels3X3cube[*iter]-1];

Work this through, simplifying things. Think about what each line is
trying to. For example, the loop is meant to iterate over the list
you've identified above.

for (list<int>::iterator
itt=ints.begin(); itt!=ints.end(); itt++ )
{
...

Then you should find things work. What was going wrong before?
You'll find, in this case and most, that it's the line where you
_change_ something that breaks something...
labels3X3cube[*itt] = currentLabel;

Me thinks you were blatting over the numbers providing the index to
keep you iterating in the "current" list...

Tony
 
T

terminator

*(labelLists[labels3X3cube[*iter]-1].end())

- Dereferencing end() is not a good idea - it is iterator pointing
beyond last valid element

I think he just meant 'rbegin'(read reverse begin):

*(labelLists[labels3X3cube[*iter]-1].rbegin())

regards,
FM.
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top