Vector Iterator | | |
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, | | | | 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 | | | | 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:
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:
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. | | | | 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) | | | | 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. | | | | 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) | | | | 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 | | | | 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. | | | | 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) |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|