473,883 Members | 2,177 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
13 4930

"Adam Hartshorne" <or********@yah oo.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********@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
>followin g 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;
}
}


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********@yah oo.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********@yah oo.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
3708
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
1388
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
2022
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
2689
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
5731
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
2598
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
6249
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
2012
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
2295
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
9948
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11164
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
10767
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
10864
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
9588
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
7980
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
7137
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();...
0
6008
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.