1ListNode
template<class T>struct ListNode{public:ListNode(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}//private://共有可访问ListNode<T>* _next;ListNode<T>* _prev;T _data;};
实现iterator对Node*的封装 实现运算符重载
void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针 it内的_node 的
{MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动while (it != lt.end()){//报错,必须是可修改的左值 *it += 19;//*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}
2ListIterator
template<class T, class Ref, class Ptr>class ListIterator{public:typedef ListNode<T> Node;typedef ListIterator<T, Ref, Ptr> Self;//变化ListIterator(Node* node):_node(node){}Ref operator*()//变化{return _node->_data;}Ptr operator->()//变化{return &(_node->_data);}Self& operator++(){_node = _node->_next;return *this;}Self operator++(int){Self tmp = *this;_node = _node->_next;return tmp;}Self& operator--(){_node = _node->_prev;return *this;}Self operator--(int){Self tmp = *this;_node = _node->_prev;return tmp;}bool operator!=(Self it){return _node != it._node;}//private: 共有Node* _node;};
3list
template<class T>class list{public:typedef ListNode<T> Node;//typedef ListIterator<T> iterator;//typedef ListConstIterator<T> const_iterator;//但这样要实现两个类 可以合并typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const T*> const_iterator;iterator begin()//头是head下一个节点{return _head->_next;//单参数的自定义类型支持隐式类型转化 c++11对于多参数的也可以{xx,xx,xx}}iterator end()//尾是head{return _head;}const_iterator begin()const//直接加const 对于const对象能调用,然后刚好构成函数重载,{return _head->_next;//返回值也不能是 const itertor 这指的是it不能改,应该封装成(*it)不可改}const_iterator end()const{return _head;}void empty_init(){//先new一个节点_head = new Node;_head->_next = _head;_head->_prev = _head;}list(initializer_list<T>il){empty_init();for (auto& e : il)push_back(e);}list(){empty_init();}list(const list<T>& lt){empty_init();for (auto& i : lt)//要引用 有可能是自定义类型{push_back(i);}}/*void push_back(const T& x){Node* newnode = new Node(x);Node* tail = _head->_prev;tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}*/void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(--end());//不能是-1,没有重载}void pop_front(){erase(begin());}void insert(iterator pos, T val){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(val);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;}iterator erase(iterator pos)//迭代器失效 返回next{Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;//return iterator(next);}size_t size(){size_t sz = 0;for (auto i : (*this))sz++;return sz;}bool empty(){return size() == 0;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}~list(){clear();delete _head;}private:Node* _head;};
4对于const迭代器的解释和->操作符重载
struct A
{int _a1;int _a2;A(int a1 = 0, int a2 = 0):_a1(a1), _a2(a2){}
};
void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改(
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针
{MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动while (it != lt.end()){//报错,必须是可修改的左值 *it += 19;//*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}
void print(MyList::list<int>& lt)
{MyList::list<int>::iterator it = lt.begin();while (it != lt.end()){*it += 19;cout << (*it) << ' ';it++;}cout << endl;
}
void test2()
{MyList::list<A>lt;lt.push_back({ 1,2 });//隐式类型转换,c++11lt.push_back({ 1,2 });lt.push_back({ 1,2 });MyList::list<A>::iterator it = lt.begin();while (it != lt.end()){//报错cout << (*it) << " ";//(*it)访问的是 节点的数据 对于int 数据就是int 支持流输出 ,这里是A对象//其实就是把it当作 指向A的指针,但本质不是 (*it)就是A //就是(*it)经过封装,(*it)就是A ,,但it->没封装//cout << (*it)._a1 << "." << (*it)._a2<<endl;//但本应该it->data 要支持->重载 返回值是_data *cout << it->_a1 << "." << it->_a2 << endl;it++;}cout << endl;
}