473,383 Members | 1,716 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,383 software developers and data experts.

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,
Jun 27 '08 #1
8 1764
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
Jun 27 '08 #2
On 22 Apr, 18:50, Erik Wikström <Erik-wikst...@telia.comwrote:
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
Thanks very much, works fine.
Jun 27 '08 #3
On 2008-04-22 13:50:53 -0400, Erik Wikström <Er***********@telia.comsaid:
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();
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)

Jun 27 '08 #4
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.comsaid:
...
Or

it->myclassmember();
...

Is that possible? myvector is a vector of pointers.

Regards.
Jun 27 '08 #5
On 2008-04-22 16:38:22 -0400, al**********@gmail.com said:
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.

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

Jun 27 '08 #6
<al**********@gmail.comwrote in message
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.com>
said:
it->myclassmember();
Is that possible? myvector is a vector of pointers.
No. Probably Pete was in a hurry.

--
http://techytalk.googlepages.com
Jun 27 '08 #7
Pete Becker wrote:
On 2008-04-22 16:38:22 -0400, al**********@gmail.com said:
>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!

--
Ian Collins.
Jun 27 '08 #8
On 2008-04-22 17:14:59 -0400, Ian Collins <ia******@hotmail.comsaid:
Pete Becker wrote:
>On 2008-04-22 16:38:22 -0400, al**********@gmail.com said:
>>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)

Jun 27 '08 #9

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

Similar topics

14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
1
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made...
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.