c++ - Implementation of map - how to differ two ends of the map in iterator -
iterator implementation:
class iterator { private: node<pair>* _ptr = nullptr; public: iterator(node<pair>* ptr = nullptr) : _ptr(ptr) { } iterator(const iterator& itr) = default; ~iterator() = default; iterator& operator=(const iterator&) = default; iterator operator++(int) { node<pair> *cur = this->_ptr; if (this->_ptr) { this->_ptr = _ptr->next(); } return cur; } iterator& operator++() { if (this->_ptr) { this->_ptr = _ptr->next(); } return *this; } pair& operator*() { if (!this->_ptr) { throw mapelementnotfoundexception(); } return this->_ptr->data(); } bool operator==(const iterator& itr) const { if ((this->_ptr == nullptr && itr._ptr == nullptr) || (this->_ptr == itr._ptr)) { return true; } return false; } friend bool operator!=(const iterator& itr1, const iterator& itr2) {///// non member return !(itr1 == itr2); } };
private of map class:
node<pair> *_head; valuetype _default; ///////////////////////////////////////////////// int _size;
the methods begin , end:
iterator begin() const{ return iterator(this->_head); } iterator end() const{ return iterator(nullptr); ////////////////////////////////////////////// }
how can implement iterator , end method work:
assert_equals(false, map9.end() == map7.end());
my goal implement iterator in such way, in implement correctly operator == , != iterators, 2 iterators identical if point same object in same map, , different in other way.
edit: (solution)
private: node<pair>* _ptr/* = nullptr */; mtmmap<valuetype, keytype, comparefunction>* _map_ptr; public: iterator(node<pair>* ptr/* = nullptr*/,const mtmmap<valuetype, keytype, comparefunction> * mtm_ptr) : _ptr(ptr), _map_ptr(mtm_ptr){ }
why compiler says there here const-correctnes problem. can't cast const mtmmap * mtm_ptr mtmmap * mtm_ptr. copy of ptr.
Comments
Post a Comment