473,700 Members | 3,078 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 4910
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::setU pAnimation() {

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::setU pAnimation() {

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********@yah oo.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::setU pAnimation() {

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********@yah oo.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::setU pAnimation() {

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::setU pAnimation() {

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::setU pAnimation()
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********@yah oo.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::setU pAnimation() {

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********@yah oo.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

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

Similar topics

38
3666
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 mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
0
1381
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 indexing was used, I could do something like child = hole * 2 + 1; but since only thing the function accepts are random access Iterators, how do I calculate the Iterator to the child node? template <typename Iterator, typename Functor> void...
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 Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
2
2008
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 traversing on specific data structures. For each iterator I want to be able to specify the four possible const combinations, corresponding to the various void* for pointers, with the possibility to perform conversions from non-const to const just like...
0
2676
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
21
5710
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 classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
16
2578
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 totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
1
6238
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 library from the higher level classes. This low level class is used to allocate locked pages of memory before doing a DMA. The third party library locks memory and returns a pointer to an internal structure, which we can simply cast to a (void...
4
2003
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 strange. The implementation looks like this (there is a lot of code, but I have only included it all for consistency. The problem is quite small and local): template<typename T> class Row<T> : public std::vector<T> {}; template<typename...
5
2285
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 from a function and b = iter() # these two are just examples.
0
8639
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9203
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9058
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8952
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7794
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5895
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2018
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.