473,830 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dereferencing (pointer to) iterator

Suppose we have a vector:
vector<intvec(1 0);

We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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 4555
xg****@gmail.co m wrote:
Suppose we have a vector:
vector<intvec(1 0);

We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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.c om" <xg****@gmail.c omwrote in
news:11******** **************@ z28g2000prd.goo glegroups.com:
Suppose we have a vector:
vector<intvec(1 0);

We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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...@kostu r.netwrote:
"xgn...@gmail.c om" <xgn...@gmail.c omwrote innews:11****** *************** *@z28g2000prd.g ooglegroups.com :
Suppose we have a vector:
vector<intvec(1 0);
We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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(1 0);
vector<int>::it erator* vecItor = new vector<int>::it erator();

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.c omwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostu r.netwrote:
>"xgn...@gmail. com" <xgn...@gmail.c omwrote
innews:11***** *************** **@z28g2000prd. googlegroups.co m:
Suppose we have a vector:
vector<intvec(1 0);
We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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(1 0);
vector<int>::it erator* vecItor = new vector<int>::it erator();
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.co m wrote:
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostu r.netwrote:
>"xgn...@gmail. com" <xgn...@gmail.c omwrote
innews:11***** *************** **@z28g2000prd. googlegroups.co m:
Suppose we have a vector:
vector<intvec(1 0);
We can declare a iterator this way:
vector<int>::it erator 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>::it erator* 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(1 0);
vector<int>::it erator* vecItor = new vector<int>::it erator();

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.c omwrote...
>vector<intvec( 10);
vector<int>::i terator* vecItor = new vector<int>::it erator();

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.co m wrote:
On Jun 22, 6:04 pm, Andre Kostur <nntps...@kostu r.netwrote:
>"xgn...@gmail. com" <xgn...@gmail.c omwrote
innews:11***** *************** **@z28g2000prd. googlegroups.co m:
>>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(1 0);
vector<int>::it erator* vecItor = new vector<int>::it erator();

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>::it erator
vecItor;

Thank you for your help guys!

Jun 23 '07 #9
xg****@gmail.co m 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>::it erator
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3047
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 using ::pointer? I did a quick experiment and replaced all the ::iterator in my code with ::pointer and it seems to work the same. What I'm think is on the surface they're the same but perhaps there're some subtle differences beneath the...
10
1910
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> using std::cout;
51
3200
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
9016
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 (pseudocode): begin = find(...);
4
16165
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 to pass both as "void *buf" so that it can accept any data type. But since I access the buffer and try to assign its elements to another I get compile errors (I have pasted at the end). Now my question is how can I pass the i/p and o/p buffers...
28
2448
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
3209
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 always been under the impression that if I had a pointer variable, p which contained the member "first", I could access this member variable either by: (*p).first OR p->first . That is, in general I thought p->x was just shorthand for (*p).x ?
4
5238
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 = find_task_by_id(getpid()); 7. printf("%s",my->comm); error: dereferencing pointer to incomplete type
20
3239
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
0
10479
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9312
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7741
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6948
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5616
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5778
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3073
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.