C++/syntax to access member data with iterators
Expert: Eddie - 11/13/2004
QuestionI'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 :)
AnswerHello 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