473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

nested iterator

hi!.
i did this method that should perform as many iterations as Sigma
element has (in the example just two: a, b), for each state (outer
bucle).

vector<_t_> * Mealy::to_deltaMoore() {
vector<_d_>::iterator i;
vector<char>::iterator j;
vector<_d_> vt; // lista de los estados Q útiles
vector<_t_> *v = new vector<_t_>; // vector de transiciones delta
_d_ d0 = {q0, ' '};
_d_ d;
_t_ t;
char q;

//! init state
vt.push_back(d0);

for(i = vt.begin(); i!= vt.end(); i++) // state q
for(j = Sigma.begin(); j!= Sigma.end(); j++){ // input
alphabeth symbol
cout << "q = " << i->a << " con el simbolo: "
<< *j << endl;
d.a = func_delta(i->a, *j); d.b =
func_lambda(i->a, *j);
cout << "delta: " << d.a << " lambda: " << d.b
<< endl;

if(!find(vt, d) && d.a != 0x0)
vt.push_back(d); // <<<---- adding some
elements to outer bucle

t.a = position(vt, *i); t.b = *j; t.c =
position(vt, d);
v->push_back(t);
}
return v;
}

the problem is that i iterator gets mad, and change into the inner
bucle (j iterator) without chaging it! That's the output of the above
method:

q = 1 con el simbolo: a
delta: 2 lambda: #
q = 0 con el simbolo: b <<-- q should be 1!!.. it changes automagically
delta: lambda:
q = con el simbolo: a <<-- q should be 2 (because 2# was add previous
iteration)
delta: lambda:
q = con el simbolo: b <<-- ...
delta: lambda:
....

is that a known bug.. or i should continue looking for the bug into my
method?

thanks in advance

Jan 9 '06 #1
1 2489
Gonzalo Aguirre wrote:
[...]
for(i = vt.begin(); i!= vt.end(); i++) // state q
[...]
if(!find(vt, d) && d.a != 0x0)
vt.push_back(d); // <<<---- adding some
elements to outer bucle
[...]
the problem is that i iterator gets mad, and change into the inner
bucle (j iterator) without chaging it!
When you 'push_back' into 'vt', it is allowed to _reallocate_. That makes
any iterators (and pointers, and references) to any elements of the vector
*invalid*. Using an invalid iterator causes undefined behaviour. So, any
time you try to dereference or even increment an invalid iterator, the
program is allowed to do as it pleases and not as you think.

Do not use iterator. Use an index.
[..]

is that a known bug.. or i should continue looking for the bug into my
method?


It's not a bug. At least not in the behaviour of 'std::vector'. It is
a bug in your understanding of what to expect.

V
Jan 9 '06 #2

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

Similar topics

0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
3
by: Chad E. Dollins | last post by:
Hello someone said that I should check this forum out for help to my c++ problems. I will admit at this point I may have really ran myself into the ground with this code that I am writing, but...
3
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general...
3
by: Gerhard Pfeiffer | last post by:
Hi, I'm trying to implement a data-structure and have an iterator for it. Now I've got a problem impleneting the operator+. I tried to isolate the problem: template<int DIM, typename Tclass...
4
by: bigbarn | last post by:
Hi - I can't seem to figure this one out, help is appreciated: Header file: template <class T> class Foo { T val; }; template<class T> class Bar {
4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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
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...
0
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
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...

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.