By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Dereferencing (pointer to) iterator

P: n/a
Suppose we have a vector:
vector<intvec(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!

Jun 22 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
xg****@gmail.com wrote:
Suppose we have a vector:
vector<intvec(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #2

P: n/a
"xg****@gmail.com" <xg****@gmail.comwrote in
news:11**********************@z28g2000prd.googlegr oups.com:
Suppose we have a vector:
vector<intvec(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.
Jun 22 '07 #3

P: n/a
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostur.netwrote:
"xgn...@gmail.com" <xgn...@gmail.comwrote innews:11**********************@z28g2000prd.google groups.com:
Suppose we have a vector:
vector<intvec(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.
Thanks guys. That makes perfect sense. However, when I run this in
Visual Studio,

vector<intvec(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.

Jun 23 '07 #4

P: n/a

<xg****@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostur.netwrote:
>"xgn...@gmail.com" <xgn...@gmail.comwrote
innews:11**********************@z28g2000prd.googl egroups.com:
Suppose we have a vector:
vector<intvec(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.

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

vector<intvec(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).
{
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.


Jun 23 '07 #5

P: n/a
xg****@gmail.com wrote:
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostur.netwrote:
>"xgn...@gmail.com" <xgn...@gmail.comwrote
innews:11**********************@z28g2000prd.googl egroups.com:
Suppose we have a vector:
vector<intvec(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.

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

vector<intvec(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
Jun 23 '07 #6

P: n/a
Jim Langston wrote:
<xg****@gmail.comwrote...
>vector<intvec(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.
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?
>for (*vecItor = vec.begin(); *vecItor != vec.end(); vecItor++)

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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 23 '07 #7

P: n/a
xg****@gmail.com wrote:
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostur.netwrote:
>"xgn...@gmail.com" <xgn...@gmail.comwrote
innews:11**********************@z28g2000prd.googl egroups.com:
>>Suppose we have a vector:
vector<intvec(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.

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

vector<intvec(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '07 #8

P: n/a
... (*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!

Jun 23 '07 #9

P: n/a
xg****@gmail.com wrote:
> ... (*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,
By "nonsense" I didn't mean a pointer to the iterator. I meant the
"managed C++".
the
compiler doesn't allow me to declare like this: vector<int>::iterator
vecItor;

Thank you for your help guys!
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '07 #10

P: n/a
<xg****@gmail.comwrote in message
news:11*********************@c77g2000hse.googlegro ups.com...
> ... (*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!
"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.
Jun 24 '07 #11

P: n/a
On 2007-06-24 09:11, Jim Langston wrote:
<xg****@gmail.comwrote in message
news:11*********************@c77g2000hse.googlegro ups.com...
>> ... (*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!

"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<intl = gcnew List<int>(); all over the place.

--
Erik Wikström
Jun 24 '07 #12

P: n/a
On Jun 22, 11:55 pm, "xgn...@gmail.com" <xgn...@gmail.comwrote:
Suppose we have a vector:
vector<intvec(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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 24 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.