473,402 Members | 2,050 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,402 software developers and data experts.

Dereferencing (pointer to) iterator

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
12 4499
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
"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
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

<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
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
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
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
... (*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
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
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs...
10
by: fabio de francesco | last post by:
Hi what do you think of the following? Why are we permitted to do that? And why the C++ Library doesn't stop someone willing to perfom that assignement (*a = 20)? #include <iostream> ...
51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
2
by: Matthias Kaeppler | last post by:
Hello, I was wondering, does dereferencing past-the-end iterators yield undefined behavior? Especially, is the result of calling an STL algorithm on an empty range undefined? For example...
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.