模板顺序表
来源:互联网 发布:单片机原理及应用pdf 编辑:程序博客网 时间:2024/06/06 08:31
用模板写函数或类都与类型无关,因此,STL中都是用模板实现容器,下面我们来介绍用模板实现顺序表。
关于模板的知识,在之前的博客中有提到:
http://blog.csdn.net/gatieme/article/details/51383272
#include<iostream>using namespace std;#include<string>//模板顺序表template<class T>class Vector{ typedef T* Iterator; typedef const T* ConstIterator;public: Vector() :_start(0) , _finish(0) , _endofstorage(0){} Vector(const T* arr, size_t size)//非类型参数模板类构造函数 :_start(new T[size]) , _finish(_start) , _endofstorage(_start + size) { for (size_t idx = 0; idx < size, ++idx) { _start[idx] = arr[idx]; } } Vector(const Vector<T> &v)//拷贝构造函数 :_start(new T[v._endofstorage - v._start]) { size_t size = v._finish - v._start; size_t capacity = v._endofstorage - v._start; _start = new[capacity]; _endofstorage = _start + capacity; _finish = _start + size; for (size_t idx = 0; idx < size; ++idx) { _start[idx] = v._start[idx]; } } Vector <T>& operator = (const Vector <T> &v)//赋值运算符的重载 { if (this != &v) { Vector<int>temp(v); swap(temp); } return *this; } void swap(Vector<T> &v) { std::swap(_start, v._start); std::swap(_finish, v._finish); std::swap(_endofstorage, v._endofstorage); } ~Vector() { if (_start) { delete[] _start; _start = 0; _finish = 0; _endofstorage = 0; } } Iterator End() { return _finish; } Iterator Begin() { return _start; } ConstIterator Begin() { return _start; } ConstIterator End()const { return _finish; } size_t size()const//大小 { return _finish - _start; } size_t Capacity()const//容量 { return _endofstorage - _start; } T& operator[](size_t index) { return _start[index]; } const T& operator[](size_t index)//下标 { return _start[index]; } T& Back() { return *(--End); } const T& Back()//最后一个元素 { return *(--End); } void checkcapacity()//检查容量 { if (_finiah == _endofstorage) { size_t capacity = 2 * (_endofstorage - _start) + 3; T* temp = new T [Capacity]; size_t = 0; T* pos = _start; while (pos != _endofstorage) { temp[idx++] = pos++; delete[] _start; _start = temp; _finish = _start + idx; _endofstorage = _start + capacity; } } T& front()//第一个元素 { return *Begin; } void PushBack(const T& x)//后插 { checkcapacity(); *_finish = x; } void PopBack()//后删 { --finish; } Iterator Insert(Iterator pos, const T& x)//任意位置插入 { checkcapacity(); Iterator it = _finish; while (it > pos) { *it = *(it - 1); --it; } *pos = x; _finish++; return pos; } Iterator Erase(Iterator pos)//任意位置删除 { Iterator it = pos; while (it < (_finish - 1)) { *(it) = *(it + 1); ++it; } --finish; return pos; } }private: T * _start; T * _finish; T * _endofstorage;};
1 0
- 模板顺序链表
- 顺序表模板C++
- 模板实现顺序表
- 顺序表模板
- 模板实现顺序表
- C++模板顺序表
- 模板实现顺序表
- 模板顺序表
- 顺序表类模板
- 顺序表---模板
- 模板实现顺序表
- 顺序表类模板
- 顺序表类模板
- 链表模板、队列模板、顺序表模板、栈模板、
- 顺序表SequentialList模板类
- C++模板实现顺序表
- 顺序表 C++模板实现
- C++模板实现顺序表
- 图片加载比较好的两种方式
- android基础学习4————Activity的存活
- CSS 水平和垂直居中对齐
- 变形课 HDU
- 看着大本营 坚持写博客
- 模板顺序表
- 二分搜索的递归实现算法
- Android分组悬浮列表实现
- [LeetCode]283. Move Zeroes
- Spring:基于注解的Spring MVC(下)
- 多线程
- bzoj 2539: [Ctsc2000]丘比特的烦恼 (KM算法)
- 用于FUZZ测试的程序及其详解
- 栈的基本操作和实现C++模板类