vector与list使用与剖析1
#pragma once #includeusing namespace std; #include #include void TestVector() { vector
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 ::iterator iter = v.begin(); for (; iter != v.end(); ++iter) { cout << *iter << " "; } cout << endl; } void TestList() { list 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 ::iterator iter = l.begin(); for (; iter != l.end(); ++iter) { cout << *iter <<" "; } cout << endl; }
仿库中迭代器的实现:
专注于为中小企业提供成都网站设计、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业五华免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
#pragma once #include#include #include #include
using namespace std; template struct __ListNode { T _data; __ListNode * _prev; __ListNode * _next; __ListNode(const T& x = T()) :_prev(NULL) , _next(NULL) , _data(x) {} }; template struct __ListIterator { typedef __ListIterator Self; typedef T ValueType; typedef Ref Reference; typedef Ptr Pointer; __ListNode * _node; __ListIterator() {} __ListIterator(__ListNode * 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 List { typedef __ListNode Node; Node* _head; public: typedef __ListIterator Iterator; typedef __ListIterator 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 l; l.PushBack(1); l.PushBack(2); l.PushBack(3); l.PushBack(4); l.PushBack(5); List ::Iterator iter = l.Begin(); while (iter != l.End()) { cout << *iter << " "; ++iter; } cout << endl; iter = l.Begin(); while (iter != l.End()) { List ::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 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 ::iterator iter = v.begin(); for (; iter != v.end(); ++iter) { cout << *iter << " "; } cout << endl; }
文章标题:vector与list使用与剖析1
浏览路径:http://ybzwz.com/article/joesgp.html