std::list<myClass *> l;// push something into the list, then...std::list<myClass *>::iterator it=l.begin ();myClass *ptr = *it;myClass &ref = **it;ptr->memFunc ();ref.memFunc ();
iterators to a list of class pointers ...
Or in other words:
Quote:Original post by leiavoia
iter->Function();
// call the function of the iterator
That's not true. iter->Function calls "Function" on the contained type. For instance, if you had a list<myClass>::iterator, iter->Function () calls myClass::Function.
Iterators actually do have functions, and you call them with the . operator. For instance, std::reverse_iterator has a member function "base" which returns a forward iterator sort-of from the current position (it's very complicated), but you call it as follows:
list<myClass*>::reverse_iterator rIt = l.rbegin ();// .. do some stuff..(*rIt)->Function (); // call a myClass functionlist<myClass*>::iterator it;it = rIt.base (); // returns a bidirectional iterator one before wher rIt was pointing
Now, I suggest never converting from reverse to forward iterators if you can ever help it, but this is how you call a function of an iterator.
Quote:Original post by StoffelQuote:Original post by leiavoia
iter->Function();
// call the function of the iterator
That's not true. iter->Function calls "Function" on the contained type. For instance, if you had a list<myClass>::iterator, iter->Function () calls myClass::Function.
Iterators actually do have functions, and you call them with the . operator. For instance, std::reverse_iterator has a member function "base" which returns a forward iterator sort-of from the current position (it's very complicated), but you call it as follows:
*** Source Snippet Removed ***
Now, I suggest never converting from reverse to forward iterators if you can ever help it, but this is how you call a function of an iterator.
mostly true, but it gets weirder, consider this:
std::map< std::string, std::string >::iterator iter = some_map.begin();std::string a_string = iter->second; // note syntax
Hi,
Well, it's really not so weird if you think about it. [smile] I mean, what is the map really? You can think of it as being equivalent to a list of pairs:
Now, if you have an iterator for that map, then what are you really iterating over? That's right, the pairs. So, when you say:
That is really equivalent to saying:
Then that makes sense, because what is *iter? It's just a pair. [smile]
Of course, if you wanted to implement your own map as a list of pair pointers, then you could do:
And then when you iterate over the map, to reference the value, you'd have to do:
Talk about weird. [wink]
Vovan
Quote:Original post by leiavoia
mostly true, but it gets weirder, consider this:
*** Source Snippet Removed ***
Well, it's really not so weird if you think about it. [smile] I mean, what is the map really? You can think of it as being equivalent to a list of pairs:
std::map<key_type, value_type> === std::vector< std::pair<key_type, value_type> >
Now, if you have an iterator for that map, then what are you really iterating over? That's right, the pairs. So, when you say:
value_type v = iter->second
That is really equivalent to saying:
value_type v = (*iter).second;
Then that makes sense, because what is *iter? It's just a pair. [smile]
Of course, if you wanted to implement your own map as a list of pair pointers, then you could do:
typedef mymap std::vector< std::pair<key_type, value_type>* >;
And then when you iterate over the map, to reference the value, you'd have to do:
value_type v = (*iter)->second;
Talk about weird. [wink]
Vovan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement