一个简单的seqlist的模板实现
来源:互联网 发布:网络金融超市 编辑:程序博客网 时间:2024/04/28 22:52
首先是 seqlist.h
template <typename T>class seqlist{public: seqlist(int capacity); ~seqlist(); int get_length(); //获取长度 int get_capacity();//获取容量 bool insert(int pos, T& t); //在pos位置插入一个元素 bool del(int pos, T& t); //在pos位置删除一个元素 T& at(int pos); //在pos位置获取一个元素private: int capacity; //容量 int length; //长度 T *p_array;};
然后是seqlist.cpp
#include "seqlist.h"template <typename T>seqlist<T>::seqlist(int capacity){ p_array = new T[capacity]; this->capacity = capacity; this->length = 0;}template <typename T>seqlist<T>::~seqlist(){ delete[] p_array; p_array = NULL; capacity = 0; length = 0;}template <typename T>int seqlist<T>::get_length() //获取长度{ return this->length;}template <typename T>int seqlist<T>::get_capacity()//获取容量{ return this->capacity;}template <typename T>bool seqlist<T>::insert(int pos, T& t) //在pos位置插入一个元素{ int i; for (i = length; i > pos; i--) { p_array[i] = p_array[i - 1]; } p_array[i] = t; //对象复制!! this->length++; return true;}template <typename T>bool seqlist<T>::del(int pos, T& t) //在pos位置删除一个元素{ if (pos <0 || pos >= get_length()) return false; t = p_array[pos]; //对象复制!! for (int i = get_length()-1; i > pos; i--) { p_array[i-1] = p_array[i]; } this->length--; return true;}template <typename T>T& seqlist<T>::at(int pos) //在pos位置获取一个元素{ return p_array[pos];}
一个测试文件
#include<iostream>#include<list>using namespace std;#include "seqlist.cpp"struct Teacher{public: int age; char name[32]; Teacher(){} Teacher(int a, char* _name) { age = a; strcpy_s(name, _name); }};int main(){ seqlist<Teacher> tlist(10); Teacher t1(10,"张三"), t2(20, "李四"), t3(30,"王五"), t4(40, "孙六"); tlist.insert(0, t1); tlist.insert(0, t2); tlist.insert(0, t3); tlist.insert(0, t4); Teacher tmp; cout << "tlist.at()" << endl; for (int i = 0; i < tlist.get_length(); i++) { tmp = tlist.at(i); cout << tmp.name << " :" << tmp.age << endl; } cout << "tlist.del()" << endl; tlist.del(2,tmp); cout << tmp.name << " :" << tmp.age << endl; tlist.del(0,tmp); cout << tmp.name << " :" << tmp.age << endl; cout << "tlist.capacity():" <<tlist.get_capacity()<< endl; system("pause"); return 0;}
还未完善。。。
0 0
- 一个简单的seqlist的模板实现
- SeqList(顺序表的实现)
- SeqList 顺序表的实现
- SeqList 顺序表的静态实现
- 1.1顺序表(SeqList)的实现
- 链表线性表的c语言实现方式 seqlist.h 和 seqlist.c
- 循序表的(SeqList.c)(函数实现)
- 一个自己实现的简单的智能指针模板类
- 一个实现了iterator模式的简单模板类
- 一个利用CAN实现车辆控制的简单模板
- 一个简单的latex模板
- 一个简单的链表类模板
- 一个简单的模板数组
- 一个简单的模板引擎
- hashtable模板的简单实现
- 基于SeqList的商品管理系统
- 循序表的(SeqList.h)
- 一个简单的类模板的使用
- java中的代码块执行顺序
- 正则--强密码校验
- angularjs笔记
- JS手撸数据结构系列(一) ——从快排搞起
- HDU 1231 最大连续子序列(DP)
- 一个简单的seqlist的模板实现
- const char*, char const*, char*const的区别
- 项目中 Android ORMLite 框架的入门用法
- bzoj2599: [IOI2011]Race
- sklearn中的nearest neighbor
- HDU 4862 Jump (费用流)
- javaScript学习日志1——面向对象
- Sql语句汇总
- 4月23日,TestSort,每日20行。