You are here:

C++/syntax to access member data with iterators

Advertisement


Question
I'm having a hard time figuring out how to access member data or functions with iterators. As in I have created a vector of pointers to ProductionRun run objects and I'm trying to figure how to use an iterator to access the member data and functions.

This is what I would think it would be:
Tally += *iter -> lifetime;

lifetime being a public data member of the ProductionRun class.

.... hope I have made this clear. I may be totally on the worng track and am needing to use an algorythm but I cant find any resources on this either.





Here is what I was doing and worked:

double calcLifeTimeMean ()
{

double Tally = 0;
int ListSize = RunList.size();
for (int j = 0; j < ListSize; j++)

{

Tally += RunList[j]->lifetime;
}

return (Tally / RunList.size());
}

RunList being a vector of POINTERS to the ProductionRun class. The reason I was using
Tally += *iter -> lifetime;
was because I was think that I also needed to dereference the pointer to the production run class contained within the vector. These are the statements that create the vector and the iterator.

vector <ProductionRun *> RunList;
vector <ProductionRun *> :: iterator iter;

ProductionRun pointers are pushed onto the vector in the batch class, as well as the function I'm trying to change...

Here is what I am using that does not work:

double calcLifeTimeMean ()
{

double Tally = 0;
int ListSize = RunList.size();
for ( iter = RunList.begin() ; iter > RunList.end() ; iter ++ )
{
Tally = Tally + (*iter).lifetime;
}

return ( Tally / RunList.size());
}

It returns this error:
batch.h: In member function 'double batch::calcLifeTimeMean()':
batch.h:63: error: request for member 'lifetime' in '(this +
12)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*() const
[with _Iterator = ProductionRun**, _Container = std::vector<ProductionRun*,
std::allocator<ProductionRun*> >]()', which is of non-class type '
ProductionRun*'


The statement "which is of non-class type '
ProductionRun*' " makes me think that the production run pointer has not yet been dereferenced.


I am using mingw build 12-12-2003

again any input would be great
Thanks :)  

Answer
Hello Nathan, thanks for the question.

You almost had it here. You can think of the iterator as a double pointer. It is a pointer to the current item in the loop, which is also a pointer. Therefore its a pointer to a pointer. When you dereference the iterator, you have the actual pointer object in the vector. As you had in the first example, you can index a vector like an array, which is why the vector is fast unlike the STL linked list. One thing I have always done when I used the iterator was to declare my for-loop a little bit different. I normally say:

for(iter = vec.begin(); iter != vec.end(); iter++)

which has always worked. So if you change your function slightly, this should work for you.


double calcLifeTimeMean ()
{

double Tally = 0;
int ListSize = RunList.size();
for(iter = RunList.begin(); iter != RunList.end(); iter++)
{
Tally = Tally + (*iter)->lifetime;
}

return ( Tally / RunList.size());
}


Also, this version would be a little bit faster since is like indexing an array, with no dereferencing needed:

double calcLifeTimeMean ()
{

double Tally = 0;
int ListSize = RunList.size();
for(int j = 0; j < ListSize; j++)
{
Tally += RunList[j]->lifetime;
}
return (Tally / RunList.size());
}


I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.