how to initialize an stl iterator in vc2005

T

thinktwice

std::vector<CString> vec;
vec::iterator iter;

i can initiaze the iterator in vc6 like this $B!'(B iter = NULL, but it
compile failed in vc2005, it tells me there is no acceptable
conversion.
 
S

Stefan Naewe

std::vector<CString> vec;
vec::iterator iter;

i can initiaze the iterator in vc6 like this : iter = NULL, but it
compile failed in vc2005, it tells me there is no acceptable
conversion.

How true...
Repeat after me: "An iterator is not a pointer".

Try this:


std::vector<CString> vec;
std::vector<CString>::iterator iter = vec.end();



What problem do you try to solve?


Regards,
Stefan
 
T

thinktwice

thanks for your reply, i want to use a class data member whhose type
is a iteraotr, i might use in several class methods, but before use it
i want to know whether this iterator is null or invalid
 
K

Kai-Uwe Bux

thinktwice said:
thanks for your reply, i want to use a class data member whhose type
is a iteraotr, i might use in several class methods, but before use it
i want to know whether this iterator is null or invalid

(a) Please don't top-post.

(b) Generally, it is impossible to determine through inspection whether an
iterator is invalid. The point is that operations on the underlying
container can invalidate iterators. The iterator is not required to detect
that.

(c) With a lot of care, you could use the container.end() value to indicate
an invalid iterator. However, this is fragile because of the problems
mentioned in (b).

(d) It might be better to consider a redesign that avoids the need for
checking the validity of the iterator. What is the underlying problem you
are trying to solve?


Best

Kai-Uwe Bux

PS: also please don't quote signatures.
 
J

James Kanze

std::vector<CString> vec;
vec::iterator iter;
i can initiaze the iterator in vc6 like this ? iter = NULL, but it
compile failed in vc2005, it tells me there is no acceptable
conversion.

It's undefined behavior. VC 6.0 failed to tell you about the
error; later versions do.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top