Dereferencing (pointer to) iterator

X

xgngli

Suppose we have a vector:
vector<int> vec(10);

We can declare a iterator this way:
vector<int>::iterator vecItor;

and then dereference it like this:
for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++)
{
cout << *vecItor << endl;
}

But how can we dereference the iterator if we declare it this way:
vector<int>::iterator* vecItor;

(I need to declare the iterator the second way since I'm declaring it
under a managed C++ class.)

Thanks!
 
V

Victor Bazarov

Suppose we have a vector:
vector<int> vec(10);

We can declare a iterator this way:
vector<int>::iterator vecItor;

and then dereference it like this:
for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++)
{
cout << *vecItor << endl;
}

But how can we dereference the iterator if we declare it this way:
vector<int>::iterator* vecItor;

(I need to declare the iterator the second way since I'm declaring it
under a managed C++ class.)

What's wrong with **vecItor?

V
 
A

Andre Kostur

Suppose we have a vector:
vector<int> vec(10);

We can declare a iterator this way:
vector<int>::iterator vecItor;

and then dereference it like this:
for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++)
{
cout << *vecItor << endl;
}

But how can we dereference the iterator if we declare it this way:
vector<int>::iterator* vecItor;

**vecItor.

That's a pointer-to-iterator. So you need to dereference your pointer
part, then dereference the iterator.
 
X

xgngli

**vecItor.

That's a pointer-to-iterator. So you need to dereference your pointer
part, then dereference the iterator.

Thanks guys. That makes perfect sense. However, when I run this in
Visual Studio,

vector<int> vec(10);
vector<int>::iterator* vecItor = new vector<int>::iterator();

for (*vecItor = vec.begin(); *vecItor != vec.end(); vecItor++)
{
cout << **vecItor << endl;
}

it caused debug assertion failure: vector iterators incompatible.

So I suspect there is no problem with the grammar. I just need to
search somewhere else to
find a way making VS happy.
 
K

Kai-Uwe Bux

Thanks guys. That makes perfect sense. However, when I run this in
Visual Studio,

vector<int> vec(10);
vector<int>::iterator* vecItor = new vector<int>::iterator();

for (*vecItor = vec.begin(); *vecItor != vec.end(); vecItor++)

for ( *vecItor = vec.begin(); *vecItor != vec.end(); ++ *vecItor )

However: I have a very hard time to belive that you have a valid reason to
use an iterator* instead of an iterator here. It looks just insane.

{
cout << **vecItor << endl;
}

it caused debug assertion failure: vector iterators incompatible.

So I suspect there is no problem with the grammar. I just need to
search somewhere else to
find a way making VS happy.

Best

Kai-Uwe Bux
 
J

Jim Langston

Thanks guys. That makes perfect sense. However, when I run this in
Visual Studio,

vector<int> vec(10);
vector<int>::iterator* vecItor = new vector<int>::iterator();

You declared a pointer to an iterator, yet you haven't pointed it to
anything yet. It's pointing to random memory since it's uninitialized. I
do not know if:
vecItor = new( std:vector<int>::iterator );
is legal or not. It may be, I just don't know (have never tried to new a
template before).
for (*vecItor = vec.begin(); *vecItor != vec.end(); vecItor++)

You are using some memory, which your program probably doesn't own, as an
interator. Also, you are incrementing the pointer with vecItor++, did you
mean *vecItor++ (that is, once you actually have it pointing to some memory
set aside as an interator).
 
T

Thomas J. Gritzan

Jim said:
You declared a pointer to an iterator, yet you haven't pointed it to
anything yet.

The OP sets the pointer to new'd memory, so that's no problem. The iterator
itself points to nowhere, but gets assigned in the for loop:
*vecItor = vec.begin();
Also ok here.
It's pointing to random memory since it's uninitialized. I
do not know if:
vecItor = new( std:vector<int>::iterator );
is legal or not. It may be, I just don't know (have never tried to new a
template before).

iterator is a typedef inside a template class, so why shouldn't it be legal?
You are using some memory, which your program probably doesn't own, as an
interator.

He owns the memory, since he new'd it.
Also, you are incrementing the pointer with vecItor++, did you
mean *vecItor++ (that is, once you actually have it pointing to some memory
set aside as an interator).

This is the problem. The code increments the pointer, not the iterator. It
should be:
++*vecItor
in the for loop.
 
V

Victor Bazarov

Thanks guys. That makes perfect sense. However, when I run this in
Visual Studio,

vector<int> vec(10);
vector<int>::iterator* vecItor = new vector<int>::iterator();

for (*vecItor = vec.begin(); *vecItor != vec.end(); vecItor++)

... (*vecItor)++

and, REALLY, why do you need this nonsense?
{
cout << **vecItor << endl;
}

it caused debug assertion failure: vector iterators incompatible.

So I suspect there is no problem with the grammar. I just need to
search somewhere else to
find a way making VS happy.

V
 
X

xgngli

... (*vecItor)++
and, REALLY, why do you need this nonsense?

You are right. It should be (*vecItor)++ or ++*vecItor. I need to
declare a iterator this way because in a managed C++ class, the
compiler doesn't allow me to declare like this: vector<int>::iterator
vecItor;

Thank you for your help guys!
 
V

Victor Bazarov

You are right. It should be (*vecItor)++ or ++*vecItor. I need to
declare a iterator this way because in a managed C++ class,

By "nonsense" I didn't mean a pointer to the iterator. I meant the
"managed C++".
 
J

Jim Langston

You are right. It should be (*vecItor)++ or ++*vecItor. I need to
declare a iterator this way because in a managed C++ class, the
compiler doesn't allow me to declare like this: vector<int>::iterator
vecItor;

Thank you for your help guys!

"Managed" C++ doesn't allow you to:
std::vector<int>::iterator vecItor;
??? I would chuck that piece of garbage so far out the window it would
leave orbit.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

"Managed" C++ doesn't allow you to:
std::vector<int>::iterator vecItor;
??? I would chuck that piece of garbage so far out the window it would
leave orbit.

It would, but as soon as you start playing with the classes from the
..Net framework (and why use C++/CLR if you don't?) you'll be using
managed classes and they usually don't play well on the stack, so
there'll be lots of List<int> l = gcnew List<int>(); all over the place.
 
J

James Kanze

Suppose we have a vector:
vector<int> vec(10);
We can declare a iterator this way:
vector<int>::iterator vecItor;

Modulo using a typedef, that's the only way to declare an
iterator.
and then dereference it like this:
for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++)
{
cout << *vecItor << endl;
}
But how can we dereference the iterator if we declare it this way:
vector<int>::iterator* vecItor;

That doesn't declare an iterator. It declares a pointer to an
iterator. Given that iterators have value semantics in C++, and
generally are designed to have short and restricted livetimes
(since various modifications to the underlying container can
invalidate them), it's hard to see where this would be
appropriate. Do you really want to have to write things like:

std::vector< int >::iterator* i
= new std::vector< int >::iterator( vec.begin() ) ;
while ( *i != vec.end() ) {
// ...
delete i ;
(I need to declare the iterator the second way since I'm declaring it
under a managed C++ class.)

Ask in a group about managed C++. I'm not familiar with it, but
I don't think that they would have C++ in the name if it didn't
support value types.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top