c++实现一个简易vector加强版
2011-08-08 13:54
603 查看
主要在原来的基础上引入了模板类
template<typename T>或者template<class T>均可
class Name_Of_Class { // ...... };
作用域解析符之前不再只是Name_Of_Class,而变成了Name_Of_Class<T>,在本类变量作函数参数时也有同样变化
比如构造函数
Name_Of_Class<T>::Name_Of_Class() { //...... }
复制构造函数
Name_Of_Class<T>::Name_Of_Class(const Name_Of_Class<T>& the_object) { //...... }
在每个成员函数的实现之前都要加上
template<typename T>
代码如下:
using std::cin; using std::cout; using std::istream; using std::ostream; using std::endl; template<typename T> class VectorT { public: bool operator =(const VectorT<T>&); friend bool operator ==(const VectorT<T>&,const VectorT<T>&); int capasity(); int size(); bool push_back(T); bool pop_back(); bool reserve(int); bool resize(int); bool empty(); T value_at(int); void change_value_at(int,T); void traval(); VectorT(); VectorT(int); VectorT(VectorT<T>&); ~VectorT(); private: bool full(); T *p; int count; int max_count; }; template<typename T> void VectorT<T>::traval() { for(int i=0;i<count;i++) { cout<<p[i]<<" "; } cout<<endl; } template<typename T> bool VectorT<T>::operator =(const VectorT<T>& temp) { if(max_count>=temp.count) { count=temp.count; max_count=temp.max_count; p=new T[max_count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } else { delete [] p; p=new T[temp.count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } return true; } template<typename T> bool operator ==(const VectorT<T>& data1,const VectorT<T>& data2) { bool flag=true; if(data1.count!=data2.count && flag==true) flag=false; if(flag==true) { for(int i=0;i<data1.count;i++) { if(data1.p[i]!=data2.p[i]) { flag=false; break; } } } return flag; } /*******************************************/ template<typename T> VectorT<T>::VectorT() { p=new T[50]; count=0; max_count=50; } template<typename T> VectorT<T>::VectorT(int length) { p=new T[length]; count=0; max_count=length; } template<typename T> VectorT<T>::VectorT(VectorT<T>& temp) { count=temp.count; max_count=temp.max_count; p=new T[max_count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } /*******************************************/ template<typename T> VectorT<T>::~VectorT() { delete [] p; } /*******************************************/ template<typename T> bool VectorT<T>::full() { if(count==max_count) return true; else return false; } template<typename T> bool VectorT<T>::empty() { if(count==0) return true; else return false; } /*******************************************/ template<typename T> int VectorT<T>::capasity() { return max_count; } template<typename T> int VectorT<T>::size() { return count; } /*******************************************/ template<typename T> bool VectorT<T>::reserve(int length) { if(length<=max_count) return false; T *temp; temp=new T[max_count]; for(int i=0;i<count;i++) { temp[i]=p[i]; } delete [] p; max_count=length; p=new T[max_count]; for(i=0;i<count;i++) { p[i]=temp[i]; } delete [] temp; return true; } template<typename T> bool VectorT<T>::resize(int length) { if(length<=max_count) { count=length; } else if(length>max_count) { count=length; reserve(length); } return true; } template<typename T> bool VectorT<T>::push_back(T data) { if(full()) { reserve(max_count*2); p[count++]=data; } else { p[count++]=data; } return true; } template<typename T> bool VectorT<T>::pop_back() { if(empty()) { cout<<"VectorT Empty"<<endl; exit(1); } else { count--; } return true; } /*******************************************/ template<typename T> T VectorT<T>::value_at(int i) { if(i>=count) { cout<<"Error location"<<endl; return -1; } return p[i]; } template<typename T> void VectorT<T>::change_value_at(int i,T data) { if(i>=count) { cout<<"Error location"<<endl; } p[i]=data; } /*******************************************/
相关文章推荐
- 用c++实现一个简易的vector
- c++实现vector类,并有一个排序和一个查找的泛型算法
- STL源码之实现一个简易的Vector容器
- C++自己实现一个简单地Vector
- [C++]assert的加强版——Ensure的简易实现
- [C++]assert的加强版——Ensure的简易实现
- C++利用链表模板类实现一个简易队列
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
- C++实现一个简易的内存池分配器
- 用Win32 SDK & C++实现的一个简易版局域网聊天软件
- C++: 实现一个简易的银行排号叫号系统
- c/c++实现的一个动态分配内存的结构体数组(类似vector)
- C++ 以传入vector的方式实现返回一个数组
- C++ vector容器中如何查找一个值是否在这个容器中
- 使用 RxJS 实现一个简易的仿 Elm 架构应用
- C++ 根据复合实现出 有一个 或 根据某物实现出
- 【C++】用C或C++实现void reverse(char* str)函数,即反转一个null结尾的字符串
- 开源一个C++实现的简单HTTP协议处理库
- 一个简单明了的c++ trace log 实现