您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
#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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。