Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Vector Iterator

Reply
Thread Tools

Vector Iterator

 
 
JackC
Guest
Posts: n/a
 
      04-22-2008
Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass*> myvector;
....
vector<MyClass*>::iterator it;

for(it = myvector.begin(); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember();
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Thanks alot for any help,
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      04-22-2008
On 2008-04-22 19:25, JackC wrote:
> Hi,
>
> If i have a std::vector containing pointers, then iterate through the
> vector and im slightly confused as to how i use the item from the
> iterator?
>
> for example,
>
> vector<MyClass*> myvector;
> ...
> vector<MyClass*>::iterator it;
>
> for(it = myvector.begin(); it < myvector.end(); it++)
> {
> // This doesn't work,
> **it->myclassmember();
> }
>
> What am i doing wrong? I thought the iterator is a pointer to a
> pointer in this case?


Use either

(*it)->myclassmember();

or

(**it).myclassmember();

--
Erik Wikström
 
Reply With Quote
 
 
 
 
JackC
Guest
Posts: n/a
 
      04-22-2008
On 22 Apr, 18:50, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2008-04-22 19:25, JackC wrote:
>
>
>
> > Hi,

>
> > If i have a std::vector containing pointers, then iterate through the
> > vector and im slightly confused as to how i use the item from the
> > iterator?

>
> > for example,

>
> > vector<MyClass*> myvector;
> > ...
> > vector<MyClass*>::iterator it;

>
> > for(it = myvector.begin(); it < myvector.end(); it++)
> > {
> > // This doesn't work,
> > **it->myclassmember();
> > }

>
> > What am i doing wrong? I thought the iterator is a pointer to a
> > pointer in this case?

>
> Use either
>
> (*it)->myclassmember();
>
> or
>
> (**it).myclassmember();
>
> --
> Erik Wikstrm


Thanks very much, works fine.
 
Reply With Quote
 
alasham.said@gmail.com
Guest
Posts: n/a
 
      04-22-2008
On Apr 22, 8:36 pm, Pete Becker <(E-Mail Removed)> wrote:
> On 2008-04-22 13:50:53 -0400, Erik Wikstrm <(E-Mail Removed)> said:

...
> Or
>
> it->myclassmember();
>

...

Is that possible? myvector is a vector of pointers.

Regards.
 
Reply With Quote
 
sk_usenet
Guest
Posts: n/a
 
      04-22-2008
<(E-Mail Removed)> wrote in message
On Apr 22, 8:36 pm, Pete Becker <(E-Mail Removed)> wrote:
>> On 2008-04-22 13:50:53 -0400, Erik Wikstrm <(E-Mail Removed)>
>> said:
>> it->myclassmember();


> Is that possible? myvector is a vector of pointers.


No. Probably Pete was in a hurry.

--
http://techytalk.googlepages.com


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-22-2008
Pete Becker wrote:
> On 2008-04-22 16:38:22 -0400, http://www.velocityreviews.com/forums/(E-Mail Removed) said:
>
>> On Apr 22, 8:36 pm, Pete Becker <(E-Mail Removed)> wrote:
>>> On 2008-04-22 13:50:53 -0400, Erik Wikstrm <(E-Mail Removed)> sa

>> id:
>> ...
>>> Or
>>>
>>> it->myclassmember();
>>>

>> ...
>>
>> Is that possible? myvector is a vector of pointers.
>>

>
> Try it.
>

Ant you'll get a compile error!

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I use stl vector iterator to delete a vector of pointers? zl2k C++ 27 09-07-2010 11:47 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments