std::map.find() is intended to return an map::iterator to an element it found if there is some or to the end() if not. I get BadPtr returned to me. The exactly same construct works fine in the other part of code. What is this?
class OntologyContainer {
map<string, OntologyClass*> data;
OntologyClass* last_added;
public:
class iterator : public std::iterator<bidirectional_iterator_tag, OntologyClass> {
map<string, OntologyClass*>::iterator itr;
public:
iterator(map<string, OntologyClass*>::iterator it) : itr(it) { }
...
}
iterator begin() {
return iterator(data.begin());
}
iterator end() {
return iterator(data.end());
}
iterator Find(const string & to_find) {
map<string, OntologyClass*>::iterator it = data.find(to_find);
// this is where it fails
return iterator(it);
}
map::iterator is wrapped for the sake of making operators * and -> returning OntologyClass objects and pointers respectively:
OntologyClass& operator* () {
return *(itr->second);
}
OntologyClass* operator->() {
return itr->second;
}
It might be something to do with the fact that you inherit from std::iterator<bidirectional_iterator_tag, OntologyClass>
, making your iterator value_type to be OntologyClass
, rather than a pointer to OntologyClass
, which is what your map iterator uses. How are you implementing the dereference operator?
if you can use boost I suggest using boost::transform_iterator.
This stack overflow answer has a decent example on how to do that: iterator adapter to iterate just the values in a map?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.