您好,登錄后才能下訂單哦!
#pragma once #include<iostream> using namespace std; #include<vector> #include<list> void TestVector() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(6); v.push_back(5); for (int i = 0; i < v.size(); ++i) { cout << v[i] <<" "; } cout << endl; vector<int>::iterator iter = v.begin(); for (; iter != v.end(); ++iter) { cout << *iter << " "; } cout << endl; } void TestList() { list<int> l;//雙向循環鏈表 l.push_back(1); l.push_back(2); l.push_back(3); l.push_back(4); l.push_back(6); l.push_back(5); list<int>::iterator iter = l.begin(); for (; iter != l.end(); ++iter) { cout << *iter <<" "; } cout << endl; }
仿庫中迭代器的實現:
#pragma once #include<iostream> #include<vector> #include<list> #include<assert.h> using namespace std; template<class T> struct __ListNode { T _data; __ListNode<T>* _prev; __ListNode<T>* _next; __ListNode(const T& x = T()) :_prev(NULL) , _next(NULL) , _data(x) {} }; template<class T,class Ref,class Ptr> struct __ListIterator { typedef __ListIterator<T, Ref, Ptr> Self; typedef T ValueType; typedef Ref Reference; typedef Ptr Pointer; __ListNode<T>* _node; __ListIterator() {} __ListIterator(__ListNode<T>* node) :_node(node) {} Reference operator*() { return _node->_data; } bool operator==(const Self& s) { return _node == s._node; } bool operator!=(const Self& s) { return _node != s._node; } Self& operator++() { _node = _node->_next; return *this; } Self operator++(int) { Self tmp(*this); _node = _noode->_next; return tmp; } Self& operator--() { _node = _noode->_prev; return *this; } Self operator--(int) { Self tmp(*this); _node = _node->_prev; return tmp; } }; template<class T> class List { typedef __ListNode<T> Node; Node* _head; public: typedef __ListIterator<T, T&, T*> Iterator; typedef __ListIterator<T,const T&,const T*> ConstIterator; List() :_head(new Node) { _head->_next = _head; _head->_prev = _head; } /*void PushBack(const T& x) { Node* tail = _head->_prev; Node* tmp = new Node(x); tmp->_next = _head; _head->_prev = tmp; tail->_next = tmp; tmp->_prev = tail; }*/ void PushBack(const T& x) { Insert(End(), x); } void PushFront(const T&x) { Insert(Begin(), x); } void PopBack() { Erase(End()); } void PopFront() { Erase(Begin()); } void Insert(Iterator pos, const T& x) { Node* cur = pos._node; Node* prev = cur->_prev; Node* tmp = new Node(x); tmp->_next = cur; cur->_prev = tmp; prev->_next = tmp; tmp->_prev = prev; } Iterator Erase(Iterator pos) { assert(pos!=End()); Node* prev = pos._node->_prev; Node* next = pos._node->_next; Node* del = pos._node; prev->_next = next; next->_prev = prev; delete del; return Iterator(next); } Iterator Begin() { return Iterator(_head->_next); } Iterator End() { return _head;//explicit //return Iterator(_head); } }; void TestSList() { List<int> l; l.PushBack(1); l.PushBack(2); l.PushBack(3); l.PushBack(4); l.PushBack(5); List<int>::Iterator iter = l.Begin(); while (iter != l.End()) { cout << *iter << " "; ++iter; } cout << endl; iter = l.Begin(); while (iter != l.End()) { List<int>::Iterator tmp = iter; ++tmp; if (*iter % 2 == 0) { l.Erase(iter); } iter = tmp; /*if (*iter % 2 == 0) { iter = l.Erase(iter); } else ++iter;*/ } iter = l.Begin(); while (iter != l.End()) { cout << *iter << " "; ++iter; } cout << endl; } void TestVector() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(6); v.push_back(5); for (int i = 0; i < v.size(); ++i) { cout << v[i] <<" "; } cout << endl; vector<int>::iterator iter = v.begin(); for (; iter != v.end(); ++iter) { cout << *iter << " "; } cout << endl; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。