473,499 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Std Iterator Problem

Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam
Jul 23 '05 #1
13 4889
Adam Hartshorne wrote:
Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam


Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
Adam Hartshorne wrote:
in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}


I don't see any errors in the above code per se. However, it's generally
*not* a good idea to define global variables in a header file.

My guess is that the error can be found elsewhere, e.g. calling erase.
It /can/ invalidate your iterator.

--
Peter
Jul 23 '05 #3
Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry


Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;

}

and it works fine, but like I say fails to find match the .end and hence
then loop around.

How should I do this instead?

Adam
Jul 23 '05 #4
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam


Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry


Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;

}

and it works fine, but like I say fails to find match the .end and hence
then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However, I
then go on to fill the list up. I need to really create/renew the
iterator when the list has been finished been filled, but not sure how I
can do that, and still have it globally available.

Adam
Jul 23 '05 #5

"Adam Hartshorne" <or********@yahoo.com> wrote in message
news:d7**********@wisteria.csv.warwick.ac.uk...
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:

Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when
the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry


Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;
}

and it works fine, but like I say fails to find match the .end and hence
then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However, I
then go on to fill the list up. I need to really create/renew the iterator
when the list has been finished been filled, but not sure how I can do
that, and still have it globally available.

Adam


Just make sure to set the iterator to begin() *after* filling up the list.
Also, reset it if you remove anything from the list. But why not make those
both members of the RmfSystem class? You can then use member functions of
that class to get or set any Site items using the iterator. (At least move
them out of the header, anyway.)

-Howard


Jul 23 '05 #6
Howard wrote:
"Adam Hartshorne" <or********@yahoo.com> wrote in message
news:d7**********@wisteria.csv.warwick.ac.uk...
Adam Hartshorne wrote:

Larry I Smith wrote:
Adam Hartshorne wrote:
>Hi All,
>
>I was wondering if anybody can tell me what is wrong with the following
>code,
>
>in a .h file I have
>
> std::list<std::vector<Site> > positions ;
> std::list<std::vector<Site> >::iterator itSteps ;
>
>then in the .cpp file
>
>void RmfSystem::step() {
>
> if (itSteps == positions.end()) {
>
> itSteps = positions.begin() ;
>
> } else {
>
> itSteps++ ;
>
> }
>
>}
>
>where step gets called by an animate function in another file. The idea
>that I have a vector of vector of positions and I want on each call of
>step that the iterator points at another set of positions. Then when
>the
>end of the list (well vector) is reached it loops around to the start.
>
>As it stands my program crashes, as itStep == positions.end() never
>seems to be found to be true.
>
>Any ideas what I am doing wrong?
>
>Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry
Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;
}

and it works fine, but like I say fails to find match the .end and hence
then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However, I
then go on to fill the list up. I need to really create/renew the iterator
when the list has been finished been filled, but not sure how I can do
that, and still have it globally available.

Adam

Just make sure to set the iterator to begin() *after* filling up the list.
Also, reset it if you remove anything from the list. But why not make those
both members of the RmfSystem class? You can then use member functions of
that class to get or set any Site items using the iterator. (At least move
them out of the header, anyway.)

-Howard


I have just checked, and I do set set the iterator to begin() after
filling up the list.

Also both are memebers of the RmfSystem Class in the header definition.
To achieve what you suggest, I'm not sure how to do that. Also how can
you 'reset' the iterator?

Adam
Jul 23 '05 #7
Adam Hartshorne wrote:
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:

Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when
the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry


Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;
}

and it works fine, but like I say fails to find match the .end and
hence then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However, I
then go on to fill the list up. I need to really create/renew the
iterator when the list has been finished been filled, but not sure how I
can do that, and still have it globally available.

Adam


After the container is filled do:

itSteps = positions.begin();

Perhaps you can just call your RmfSystem::setUpAnimation()
method after the container is filled.

The way you're using these globals is very risky.
It is not normally done, for good reasons. Creating
them in a header file (that may be included by multiple
source files) is a bad idea; you may end up with multiple
copies of them in multiple object files. To ensure that
you get just one copy of each global, try this:

in Site.h:

// state that the global 'positions' and 'itSteps' exist
// somewhere
extern std::list<std::vector<Site> > positions ;
extern std::list<std::vector<Site> >::iterator itSteps ;

in Site.cpp:

// create the global 'positions' and 'itSteps'
std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

Any changes to the container by any function may
invalidate the iterator.

I have no idea what you're trying to do, but perhaps
using a class to encapsulate the container , access to it,
and changes to it, would be a better approach.

Start with a good book on OO Design...

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #8
Adam Hartshorne <or********@yahoo.com> wrote in
news:d7**********@wisteria.csv.warwick.ac.uk:
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:

Hi All,

I was wondering if anybody can tell me what is wrong with the
following code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The
idea that I have a vector of vector of positions and I want on each
call of step that the iterator points at another set of positions.
Then when the end of the list (well vector) is reached it loops
around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry


Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;

}

and it works fine, but like I say fails to find match the .end and
hence then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However,
I then go on to fill the list up. I need to really create/renew the
iterator when the list has been finished been filled, but not sure how
I can do that, and still have it globally available.


Since you're playing with vector<>, you could use an index instead of an
iterator. Just be careful that your vector doesn't get _shorter_ while
you hold an index into the vector. Also watch out for iterator validity
rules. Remember that on insert, all iterators and references into the
vector may be invalidated. So... doing:

std::vector<int> vec;

// assume vec has say 5 items in it already

int index = 2;

int & vecvalue = vec[index];

vec.push_back(6);

vecvalue = 7;
Is "illegal". After the push_back, the reference vecvalue is no longer
valid. However, if you:

std::vector<int> vec;

// assume vec has say 5 items in it already

int index = 2;

int vecvalue = vec[index];

vec.push_back(6);

vec[index] = 7;

Is "correct". After the push_back, we re-index back into the vector, so
it recalculates on the potentially new area of memory that the vector now
occupies...

So, your step() method becomes:

void RmfSystem::step()
{
if (itSteps < positions.size())
{
++itSteps;
}
else
{
itSteps = 0;
}
}
Jul 23 '05 #9

"Adam Hartshorne" <or********@yahoo.com> wrote in message
>>
>>void RmfSystem::step() {
>>
>> if (itSteps == positions.end()) {
>>
>> itSteps = positions.begin() ;
>>
>> } else {
>>
>> itSteps++ ;
>>
>> }
>>
>>}
>>


You haven't shown the context in which this is used, but I think I see the
problem. If I recall correctly, the iterator end() points to one past the
last valid member of the list, *not* to the last member itself. Given that,
the function should be:

++itSteps;
if (itSteps == positions.end())
itSteps = positions.begin();
(I suppose you could pre-increment inside the if statement, but I prefer to
limit actions to one per line, personally.)

-Howard

Jul 23 '05 #10

"Adam Hartshorne" <or********@yahoo.com> skrev i en meddelelse
news:d7**********@wisteria.csv.warwick.ac.uk...
Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

*** 2

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never seems
to be found to be true.

Any ideas what I am doing wrong?

Adam

Apart from what others say, I must ask you where you "use" your iterator.
You are not allowed to use it in *** 2 indicated above as you might
dereference end().

/Peter
Jul 23 '05 #11
Andre Kostur wrote:
Adam Hartshorne <or********@yahoo.com> wrote in
news:d7**********@wisteria.csv.warwick.ac.uk:

Adam Hartshorne wrote:

Larry I Smith wrote:
Adam Hartshorne wrote:
>Hi All,
>
>I was wondering if anybody can tell me what is wrong with the
>following code,
>
>in a .h file I have
>
> std::list<std::vector<Site> > positions ;
> std::list<std::vector<Site> >::iterator itSteps ;
>
>then in the .cpp file
>
>void RmfSystem::step() {
>
> if (itSteps == positions.end()) {
>
> itSteps = positions.begin() ;
>
> } else {
>
> itSteps++ ;
>
> }
>
>}
>
>where step gets called by an animate function in another file. The
>idea that I have a vector of vector of positions and I want on each
>call of step that the iterator points at another set of positions.
>Then when the end of the list (well vector) is reached it loops
>around to the start.
>
>As it stands my program crashes, as itStep == positions.end() never
>seems to be found to be true.
>
>Any ideas what I am doing wrong?
>
>Adam

Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry
Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;

}

and it works fine, but like I say fails to find match the .end and
hence then loop around.

How should I do this instead?

Adam


I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However,
I then go on to fill the list up. I need to really create/renew the
iterator when the list has been finished been filled, but not sure how
I can do that, and still have it globally available.

Since you're playing with vector<>, you could use an index instead of an
iterator. Just be careful that your vector doesn't get _shorter_ while
you hold an index into the vector. Also watch out for iterator validity
rules. Remember that on insert, all iterators and references into the
vector may be invalidated. So... doing:

std::vector<int> vec;

// assume vec has say 5 items in it already

int index = 2;

int & vecvalue = vec[index];

vec.push_back(6);

vecvalue = 7;
Is "illegal". After the push_back, the reference vecvalue is no longer
valid. However, if you:

std::vector<int> vec;

// assume vec has say 5 items in it already

int index = 2;

int vecvalue = vec[index];

vec.push_back(6);

vec[index] = 7;

Is "correct". After the push_back, we re-index back into the vector, so
it recalculates on the potentially new area of memory that the vector now
occupies...

So, your step() method becomes:

void RmfSystem::step()
{
if (itSteps < positions.size())
{
++itSteps;
}
else
{
itSteps = 0;
}
}


No using a vector, it is a list of vectors. I am iterating through the
list. I have appeared to solve my problem, but replacing

if (itSteps == positions.end()) {

with

if (itSteps == --positions.end()) {

appears to work correctly.

Jul 23 '05 #12
Adam Hartshorne <or********@yahoo.com> wrote in
news:d7**********@wisteria.csv.warwick.ac.uk:

No using a vector, it is a list of vectors. I am iterating through the
list. I have appeared to solve my problem, but replacing

if (itSteps == positions.end()) {

with

if (itSteps == --positions.end()) {

appears to work correctly.


Undefined behaviour if positions is empty.
Jul 23 '05 #13

"Adam Hartshorne" <or********@yahoo.com> skrev i en meddelelse
news:d7**********@wisteria.csv.warwick.ac.uk...
Andre Kostur wrote:
[snip]
if (itSteps == positions.end()) {

with

if (itSteps == --positions.end()) {

appears to work correctly.

As Andre pointed out, this is undefined if the list is empty. It is also an
indication that you are using an invalid iterator as i explained in my other
reply.

/Peter
Jul 23 '05 #14

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

Similar topics

38
3637
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...
0
1365
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
26
1514
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...
2
1996
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
0
2662
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...
21
5679
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
16
2555
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...
1
6220
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
4
1988
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
5
2277
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
0
7131
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
7174
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
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
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
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.