STL ——list和vector
来源:互联网 发布:淘宝客服培训班 编辑:程序博客网 时间:2024/06/05 16:53
首先简单模拟实现List的普通迭代器
#include<iostream>#include<assert.h>using namespace std;template<class T>struct __ListNode{ T _data; __ListNode<T> *_prev; __ListNode<T> *_next; __ListNode(const T&x) :_data(x) , _prev(NULL) , _next(NULL) {}};template<class T, class Ref, class Ptr>struct __ListIterator{ typedef __ListNode<T> Node; typedef __ListIterator<T, Ref, Ptr> Self; __ListIterator(Node *x) :_node(x) {} // T& 出了作用域,对象还存在所以用Ref Ref operator*() { return _node->_data; } //T* ->返回数据地址 Ptr operator->() { return &_node->_data; } Self & operator++() { _node = _node->_next; return *this; } Self operator++(int) { Node* tmp = _node; _node = _node->_next; return tmp; } Self &operator--() { _node = _node->_prev; return *this; } Self operator--(int) { Node* tmp = _node; _node = _node->_prev; return tmp; } bool operator != (const Self& s) const { return _node != s._node; } bool operator==(const Self &s) const { return _node == s._node; } Node *_node;};template<class T>class List{public: typedef __ListNode<T> Node; typedef __ListIterator<T, T&, T*> Iterator; List() { _head = new Node(T()); _head->_prev = _head; _head->_next = _head; } //析构函数 ~List() { Clear(); delete _head; _head = NULL; } Iterator Begin() { return Iterator(_head->_next); } Iterator End() { return Iterator(_head); } void Insert(Iterator pos, const T &x) { Node *next = pos._node; Node *prev = next->_prev; Node *cur = new Node(x); cur->_prev = prev; prev->_next = cur; cur->_next = next; next->_prev = cur; } void Erase(Iterator pos) { assert(pos!=End()&&pos._node); Node* cur = pos._node; Node* prev = cur->_prev; Node* next = cur->_next; prev->_next = next; next->_prev = prev; delete cur; } bool Emply() { return _head == _head->_next; } Iterator Find(const T&x) { Iterator it = Begin(); while (it != End()) { if (*it == x) return it; it++; } return it; } void PushBack(const T&x) { return Insert(End(), x); } void PopBack() { return Erase(--End()); } void PushFront(const T&x) { return Insert(Begin(), x); } void PopFront() { return Erase(Begin()); } void Clear() { Iterator it = Begin(); while (it != End()) { Node*del = it._node; ++it; delete del; } } void Print() { Iterator it = Begin(); while (it != End()) { cout << *it << " "; cout << endl; ++it; } }protected: Node *_head;};void PrintList(List<int>& l1)//迭代器 是 类似指针的一个对象 -> 和智能指针相似 //[begin, end) 迭代器是 左闭右开 区间 { List<int >::Iterator it = l1.Begin(); while (it != l1.End()) { cout << *it << " "; it++; } cout << endl;}void TestList(){ List<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); l1.PopBack(); l1.PopBack(); l1.Print();}int main(){ TestList(); system("pause"); return 0;}
接下来实现一下vector
#pragma once#include <iostream>using namespace std;#include <assert.h>template<class T>class Vector{public : typedef T* Iterator; typedef const T* ConstIterator; Vector() :_start(NULL) , _finish(NULL) , _endOfStorage(NULL) {} size_t Size()const { return _finish - _start; } size_t Capacity()const { return _endOfStorage - _start; } void PushBack(const T& x) { Iterator end = End(); Insert(end, x); } void PopBack() { if (_start) { --_finish; } } Iterator Begin() { return _start; } ConstIterator Begin()const { return _start; } Iterator End() { return _finish; } ConstIterator End()const { return _finish; } //增容 void CheckEndOfStorage() { size_t size = Size(); if (_finish == _endOfStorage) { //1.开空间 2.赋值 3.释放旧空间 4.赋值 Iterator tmp = new T[2 * size + 3]; for (size_t i = 0; i < size; ++i) { tmp[i] = _start[i]; } delete[] _start; _start = tmp; _finish = _start + size; _endOfStorage = _start + 2 * size + 3; } } //插入(存在迭代器失效问题) void Insert(Iterator& pos,const T& value) { size_t size = pos - _start; size_t oldSize = Size(); if (pos == End() && _finish < _endOfStorage)//容量足够且尾插 { _start[oldSize] = value; ++_finish; } else//不是尾插 { CheckEndOfStorage(); Iterator it = _finish; pos = _start + size;//重置pos while (pos != it) { *it = *(it - 1); it--; } *pos = value; ++_finish; } } //将参数给成引用可以解决迭代器失效问题 Iterator Erase(Iterator& pos) { Iterator end = End(); Iterator cur = pos; while (cur != end) { *cur = *(cur + 1); cur++; } --_finish; pos--; return pos; } T& operator[](size_t index) { return *(_start + index) } const T& operator[](size_t index)const { return *(_start + index) } //capacity的值在vector中是以1/2来增长的 void Expand(size_t size) { size_t capacity = Capacity(); if (size == capacity) { capacity = capacity + capacity / 2; if (capacity < Size() + 1) { capacity = Size() + 1; } } _start = new T[capacity]; _finish = _start + size; _endOfStorage = _start + capacity; } bool Empty() { return _start == _finish; } void Print() { size_t size = Size(); for (size_t i = 0; i < size; ++i) { cout << _start[i] << " "; } cout << endl; } //析构函数 ~Vector() { if (_start) { delete[] _start; _start = NULL; _finish = NULL; _endOfStorage = NULL; } }protected : Iterator _start; Iterator _finish; Iterator _endOfStorage;};
阅读全文
0 0
- STL ——list和vector
- C++ STL:vector和list
- C++ STL:vector和list
- 《“vector+list”的使用——STL》
- 《STL源码剖析》学习笔记之三——序列式容器(list和vector)
- STL之vector和list的实例
- STL 中vector list 和deque
- STL--list和vector容器间复制
- STL deque、vector和list区别
- vector 和 list 区别 在stl中
- STL中vector、list、deque和map
- 关于STL中的Vector,list和deque
- STL顺序容器vector,list和deque
- STL中vector和list的使用
- 【数据结构】STL中的vector和list
- 【STL】STL 中vector和list的使用
- STL——vector和map
- STL:map, vector, list
- HDU-5536:Chip Factory(Trie树)
- 简单的聊一聊微信支付的那些事
- 欢迎使用CSDN-markdown编辑器
- 2017-09-21 LeetCode_032 Longest Valid Parentheses
- Round #435 (Div. 2)B. Mahmoud and Ehab and the bipartiteness(二分图染色)
- STL ——list和vector
- Spring4类属性自动装配和方法注入
- CentOS6.5安装配置Tomcat方法
- 信管16数据结构:实验二 线性表综合实验
- [ASP.NET]从ASP.NET Postback机制,到POST/GET方法
- slnklist(不带头结点的单链表)
- AngularJS 作用域与数据绑定机制
- SSH配置流程 (基于注解方式)
- AngularJS学习总结