Re: In map iterator is there a difference between (*iter).second and iter->second?
Re: In map iterator is there a difference between (*iter).second and
iter->second?
"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:8114115f-8075-4f7b-891d-298b48b9a383@56g2000hsm.googlegroups.com...
I see that a lot of former in the code, and wonder if there is a
technical reason for that
Most people suggest the second form, iter->second, which I initially used.
But then I ran across this from some class I was developing:
#ifndef OBSERVABLE_H
#define OBSERVABLE_H
#include "Observer.h"
#include <set>
class Observable {
std::set<Observer*> observers;
public:
virtual void addObserver(Observer& o) {
observers.insert(&o);
}
virtual void deleteObserver(Observer& o) {
observers.erase(&o);
}
virtual void deleteObservers() {
observers.clear();
}
virtual size_t countObservers() {
return observers.size();
}
virtual void notifyObservers(Argument* arg = NULL)
{
std::set<Observer*>::iterator it;
for(it = observers.begin(); it != observers.end(); it++)
(*it)->update(this, arg);
}
};
#endif
Look at the function virtual void notifyObservers(Argument* arg = NULL)
specifically at the line:
(*it)->update(this, arg);
When I first tried to code that I was trying to do:
it->->update(this, arg);
which, obviously, wouldn't compile. So I wound up with that format,
derefernce the iterator, then use the arrow operator. And I thought about
it, and dereferenceing the iterator would always work, where using the arrow
operator would usually work, but not always. So I determined that all code
working with iterators would be dereferecned instead of using the arrow
operator for consistancy. So a map would be
(*it).second
etc..
I know seeing (*it) in my code that I am dereferncing an iterator. In fact,
I reserve the variable it for a local iterator.
Other than that, there is no real difference, it comes down to personal
prefernce and whatever your workplace standards are. I figured on (*it)
because I could be consistant with it. Although it is said that consistancy
is the hobgoblin of little minds I still like being consistant if I can. If
that means I have a little mind, so be it.
Regards,
Little Minded Jim Langston