Connecting Tech Pros Worldwide Forums | Help | Site Map

Vector Iterator

JackC
Guest
 
Posts: n/a
#1: Jun 27 '08
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,

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Vector Iterator


On 2008-04-22 19:25, JackC wrote:
Quote:
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
JackC
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Vector Iterator


On 22 Apr, 18:50, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2008-04-22 19:25, JackC wrote:
>
>
>
Quote:
Hi,
>
Quote:
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?
>
Quote:
for example,
>
Quote:
vector<MyClass*myvector;
...
vector<MyClass*>::iterator it;
>
Quote:
for(it = myvector.begin(); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember();
}
>
Quote:
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
Thanks very much, works fine.
Pete Becker
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Vector Iterator


On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikstrom@telia.comsaid:
Quote:
On 2008-04-22 19:25, JackC wrote:
Quote:
>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();
Or

it->myclassmember();

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

alasham.said@gmail.com
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Vector Iterator


On Apr 22, 8:36 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia.comsaid:
...
Quote:
Or
>
it->myclassmember();
>
...

Is that possible? myvector is a vector of pointers.

Regards.
Pete Becker
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Vector Iterator


On 2008-04-22 16:38:22 -0400, alasham.said@gmail.com said:
Quote:
On Apr 22, 8:36 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia.comsa
id:
...
Quote:
>Or
>>
>it->myclassmember();
>>
...
>
Is that possible? myvector is a vector of pointers.
>
Try it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

sk_usenet
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Vector Iterator


<alasham.said@gmail.comwrote in message
On Apr 22, 8:36 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
Quote:
>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia.com>
>said:
>it->myclassmember();
Quote:
Is that possible? myvector is a vector of pointers.
No. Probably Pete was in a hurry.

--
http://techytalk.googlepages.com


Ian Collins
Guest
 
Posts: n/a
#8: Jun 27 '08

re: Vector Iterator


Pete Becker wrote:
Quote:
On 2008-04-22 16:38:22 -0400, alasham.said@gmail.com said:
>
Quote:
>On Apr 22, 8:36 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
>>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia.comsa
>id:
>...
Quote:
>>Or
>>>
>>it->myclassmember();
>>>
>...
>>
>Is that possible? myvector is a vector of pointers.
>>
>
Try it.
>
Ant you'll get a compile error!

--
Ian Collins.
Pete Becker
Guest
 
Posts: n/a
#9: Jun 27 '08

re: Vector Iterator


On 2008-04-22 17:14:59 -0400, Ian Collins <ian-news@hotmail.comsaid:
Quote:
Pete Becker wrote:
Quote:
>On 2008-04-22 16:38:22 -0400, alasham.said@gmail.com said:
>>
Quote:
>>On Apr 22, 8:36 pm, Pete Becker <p...@versatilecoding.comwrote:
>>>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia.comsa
>>id:
>>...
>>>Or
>>>>
>>>it->myclassmember();
>>>>
>>...
>>>
>>Is that possible? myvector is a vector of pointers.
>>>
>>
>Try it.
>>
Ant you'll get a compile error!
Yup. There's an extra level of indirection that I overlooked.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Closed Thread