模板类实现顺序表
来源:互联网 发布:淘宝收费推广有哪些 编辑:程序博客网 时间:2024/05/22 07:52
#include <iostream>using namespace std;template<class T>class Vector{ //定义几个类型,以便于管理public: typedef T ValueType; typedef ValueType* Pointer; typedef const ValueType* ConstPointer; typedef ValueType* Iterator; typedef const ValueType* ConstIterator; typedef ValueType& Reference; typedef const ValueType& ConstReference; typedef size_t SizeType;public: // 构造函数-无参数 Vector() : _start(0) , _end(0) , _endOfStorage(0) { } // 构造函数-有参数-->n个值为value的数据 Vector(SizeType n, const T& value) { FillAndInit(n, value); } //构造函数-有参数-->n个默认的元素 Vector(SizeType n) { FillAndInit(n, T()); } //拷贝构造函数 Vector(const Vector<T>& v) { _start = new T[v.Capacity()]; _end = _start + v.Size(); _endOfStorage = _start + v.Capacity(); for (size_t idx = 0; idx < v.Size(); idx++) _start[idx] = v._start[idx]; } // 赋值运算符的重载函数 Vector<T>& operator=(const Vector<T>& v) { _start = new T[v.Capacity()]; _end = _start + v.Size(); _endOfStorage = _start + v.Capacity(); for (size_t idx = 0; idx < v.Size(); idx++) { _start[idx] = v._start[idx]; } return *this; } //析构函数 ~Vector() { delete[] _start; //只为_start开辟空间了,所以只释放_start的空间 _start = NULL; } /*---操作容量的一系列函数---*/ //返回容量的大小 SizeType Capacity()const { return SizeType(_endOfStorage - Begin()); // 记得强转为SizeType型的数据再返回 } //返回此时存了多少个数据 SizeType Size()const { return SizeType(_end - _start); } //返回这个开辟的空间最多能存多少个数据 SizeType MaxSize()const { return SizeType(-1) / sizeof(T); //数据容量用SizeTyep类型的标量保存,用最大的数字除以每个数据的大小,既是可保存数据的作答容量 } //判断该对象是否为空,空则返回1,否则返回0 bool Empty()const { return (Begin() == End()); } /*---一系列操纵表中数据的函数---*/ // []的重载函数,可以直接用下标访问表中元素 Reference operator[](size_t index) { return _start[index]; } // const类型的[]的重载函数,可以直接用下标访问表中元素 ConstReference operator[](size_t index)const { return *(begin() + index); } // 采用引用返回头元素的值 Reference Front() { return *Begin(); } //采用const的引用返回头元素的值 ConstReference Front()const { return *Begin(); } // 采用引用返回尾元素的值 Reference Back() { return *(End() - 1); } //采用const的引用返回尾元素的值 ConstReference Back()const { return *(End() - 1); } /*---对于简单迭代器的操作---*/ // 返回头指针的函数,可使操作中不直接对头指针操作 Iterator Begin() { return _start; } ConstIterator Begin()const { return _start; } // 返回尾指针的函数,可使操作中不直接对尾指针操作 Iterator End() { return _end; } ConstIterator End()const { return _end; } /*---一些栈上的操作---*/public: // 入栈即尾插入一个值为value的元素 void PushBack(const T& value) { CheckCapacity(); SizeType num = Size(); _start[num] = value; _end++; cout << "PushBack Down" << endl; } //尾删一个元素 void PopBack() { if (Empty()) { cout << "Nothing To Pop" << endl; return; } --_end; cout << "Pop Dowm" << endl; } // 在position位置插入元素data Iterator Insert(Iterator position, const T& data) { CheckCapacity(); for (Iterator index = _end - 1; index >= position; index--) { *(index + 1) = *index; } *position = data; ++_end;; return position; } // 删除position位置的元素 Iterator Erase(Iterator position) { for (Iterator index = position; index < End(); index++) { *index = *(index + 1); } _end--; return position; } //为插入操作找出一个位置 Iterator Find(const T& value) { size_t i = 0; while (_start != _end) { if (value == _start[i]) { return _start + i; } i++; } cout << "This Value Doesn't Exist!" << endl << "We Return A NULL Position!" << endl; return NULL; }protected: // 填充函数,并初始化这个表 void FillAndInit(SizeType n, const T& value) { _start = new T[n]; _end = _start + n; _endOfStorage = _start + n; for (size_t idx = 0; idx < n; idx++) { _start[idx] = value; } } //判断表满与否。满则多开辟一段空间 void CheckCapacity() { if (Size() == Capacity()) { SizeType num = 2 * Capacity() + 100; Iterator ptemp = new T[num]; size_t idx = 0; for (idx = 0; idx < Size(); idx++) { ptemp[idx] = _start[idx]; } delete _start; _start = ptemp; _end = _start + idx; _endOfStorage = _start + num; } else return; } // 重载输出运算符,使之能直接输出所有元素 friend ostream& operator<<(ostream& os, Vector<T>& v) { for (T* p = v.Begin(); p < v.End(); p++) { os << *p << " "; } return os; }protected: Iterator _start; Iterator _end; Iterator _endOfStorage;};//测试函数,测试以上类中的所有函数void FunTest(){ Vector<int> v1(5, 1); cout << "v1 = " << v1 << endl; cout << "v1.size = " << v1.Size() << endl; cout << "v1.capacity = " << v1.Capacity() << endl; cout << "v1 pushback 5" << endl; v1.PushBack(5); cout << "v1 = " << v1 << endl; cout << "v1.capacity = " << v1.Capacity() << endl; cout << "v1.size = " << v1.Size() << endl; v1[1] = 10; v1[2] = 20; v1[3] = 30; v1[4] = 40; v1[5] = 50; v1[6] = 60; cout << "v1 change its members" << endl; cout << "v1 = " << v1 << endl; int* pos1 = v1.Find(10); cout << "v1 insert 100 at where 10 is" << endl; v1.Insert(pos1, 100); cout << "v1 = " << v1 << endl; int* pos2 = v1.Find(50); cout << "v1 delete 50" << endl; v1.Erase(pos2); cout << "v1 = " << v1 << endl; cout << "v1.Front = " << v1.Front() << endl; cout << "v1.Back = " << v1.Back() << endl; cout << "v1 popback until empty" << endl; size_t tmp = v1.Size(); for (size_t i = 0; i <= tmp; i++) { v1.PopBack(); } cout << "v1 = " << v1 << endl; Vector<char> v2(10, 'a'); cout << "v2 = " << v2 << endl; Vector<double> v3(7, 1.23); cout << "v3 = " << v3 << endl; Vector<char*> v4(3, "duanliming"); cout << "v4 = " << v4 << endl; Vector<double> v5(v3); cout << "v5 copy v3 " << endl; cout << "v5 = " << v5 << endl; Vector<char> v6; cout << "v6 use " << "=" << " by v2" << endl; v6 = v2; cout << "v6 = " << v6 << endl; cout << "v6.capacity = " << v6.Capacity() << endl; cout << "v6.MaxSize = " << v6.MaxSize() << endl;}int main(){ cout << " ALL THE ACTION IS TESTING" << endl; FunTest(); system("pause"); return 0;}
0 0
- 类模板实现顺序表
- 模板类实现顺序表
- 顺序表---模板类实现
- 模板类实现顺序表
- 模板类实现顺序表
- 模板实现顺序表
- 模板实现顺序表
- 模板实现顺序表
- 模板实现顺序表
- C++类模板实现顺序表
- C++模板类实现顺序表
- C++模板类实现顺序表
- c++(模板类)实现顺序表
- C++模板类实现顺序表
- 用模板类实现顺序表
- 用模板类实现顺序表
- C++模板实现顺序表
- 顺序表 C++模板实现
- Calculus Midterm
- MAC 安装Jmeter
- (51nod)1004
- Linux 中的主机、控制台和终端的起源
- 《何谓文化》摘录
- 模板类实现顺序表
- python3爬取廖雪峰大大python3教程
- 数论-魔幻六位数
- Android 面试题,持续更新!
- Caffe解读(一)
- 九度oj 1124 Digital Root
- 利用简单的过滤器 过滤特殊字符实现 防止XSS攻击
- dom4j解析xml
- ROS中给turtlebot加两个传感器:kinect+hokuyo,进行gazebo仿真 (二)