iterator problem

C

Chameleon

All these happen in VC++ NET 2003, if you ask me about the compiler.
it._Myptr is a VC++ specific member of the iterator implementation.
But from this, we extract usefull informations about crash.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0 (what pointer
is this?)
v.insert(it, 5); // works
it++; // it._Myptr == 4
v.insert(it, 5); // crashes
----------------------------
and this.
----------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin(); // it._Myptr == 0
v.push_back(5);
vector<int*>::iterator it = v.begin(); // it._Myptr == 0x00323b40 (the
pointer to first element)
----------------------------


All of these are compiler bugs on iterator implementation, or I miss
something?

Thanks
 
C

Calum Grant

Chameleon said:
All these happen in VC++ NET 2003, if you ask me about the compiler.
it._Myptr is a VC++ specific member of the iterator implementation.
But from this, we extract usefull informations about crash.

The same as v.end(). No bug. You're not going to dereference it.
v.insert(it, 5); // works

That's okay, but invalidates "it".
it++; // it._Myptr == 4

Unfortunately, "it" is invalid.
v.insert(it, 5); // crashes
Yup.

This compiles?
vector<int*>::iterator it = v.begin(); // it._Myptr == 0x00323b40
(the pointer to first element)
----------------------------


All of these are compiler bugs on iterator implementation, or I miss
something?

Ah, not every error is a compiler error!

Cheers, Calum
 
D

Daniel T.

Chameleon said:
All of these are compiler bugs on iterator implementation, or I miss
something?

Until you know enough that you can build your own compiler, go ahead and
assume that the bug is in your code rather than the compiler's code...
Just a thought.
 
C

Chameleon

Calum said:
The same as v.end(). No bug. You're not going to dereference it.


That's okay, but invalidates "it".


Unfortunately, "it" is invalid.


This compiles?

sorry said:
Ah, not every error is a compiler error!


of-course!

my solution is this until now:

-------------------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin();
it = v.insert(it, 5);
it++;
it = v.insert(it, 5);
-------------------------------------
 
L

Luke Meyers

Chameleon said:
sorry, vector<int> v;

Okay said:
my solution is this until now:

-------------------------------------
vector<int*> v;
vector<int*>::iterator it = v.begin();
it = v.insert(it, 5);
it++;
it = v.insert(it, 5);
-------------------------------------

Back to vector<int*> now? Well, in any case... line 2 above serves no
purpose. I would change this snippet to:

std::vector<int> v;
v.push_back(5);
v.push_back(5);

See? No need for iterators at all in this case. Maybe you got
confused because you didn't know about push_back? You owe it to
yourself to learn the public interface of important classes like
std::vector. It is only wafer-thin.

Luke
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top