-3

Before, when using normal for-loop I would access it like this:

this->customClassCustomVectorArray[i]->getCustomMember();

But now I dont know how to access it because when I type "->" VS2010 doesnt offer any members or methods.

for(vector<CustomClass*>::iterator it = this->customClassCustomVectorArray.begin(); it != this->customClassCustomVectorArray.end(); ++it){
    cout << this->customClassCustomVectorArray[*it]->getCustomMember() << endl;
}

Ive tried "*it" and "it" but nothing happens. Is this even possible? I suppose it should be.

3 Answers3

4
cout << this->customClassCustomVectorArray[*it]->getCustomMember() << endl;

That is not right. [] expects a numeric index into the vector, but *it is not a numeric index. It is an iterator. It already points to the correct position in the vector.

cout << (*it)->getCustomMember() << endl;

[] is only for when you are iterating using numeric indices, not iterators.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

Wrong:

this->customClassCustomVectorArray[*it]->getCustomMember()

Right:

(*it)->getCustomMember()

Why? Because it is very much like

this->customClassCustomVectorArray + i

In fact, it's exactly the same as

this->customClassCustomVectorArray.begin() + i

so you don't need to say

this->customClassCustomVectorArray

twice.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

While @Lightness's answer is correct as far as it goes, it still doesn't point toward how you should be doing this. Most times that you write an explicit loop using iterators, it's a mistake (and this is no exception). Most uses of std::endl are also mistakes (and this doesn't look like an exception in that respect either).

So, instead of writing an explicit loop, then explicitly dereferencing the iterator, etc., and finally taking his advice (correct though it is) to get the syntax correct, you should be thinking in terms of getting rid of essentially all of that, and using generic algorithms to do the job instead.

In this case, you have an input collection. For each item in that input collection, you're invoking a function, then writing the result to some output collection. That pretty much describes std::transform, so it's probably a decent fit for the job at hand.

std::transform(customClassCustomVectorArray.begin(),
               customClassCustomVectorArray.end(),
               mem_fun(&customClass::customMember),
               std::ostream_iterator<result_type>(std::cout, "\n"));

This uses result_type to represent the type of the result from invoking your member function. Having to explicitly specify that result type is something of a liability of this technique, but at least IMO, its strengths substantially outweigh that minor liability.

Probably the most obvious strength is that the definition of std::transform is (reasonably) well known and standardized, so anybody who knows C++ (at all well) immediately has a pretty fair idea of what this is supposed to do as a whole--invoke some function on every item in a range, and write the returned value to some destination.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111