用c++实现一个简易的vector
2011-08-07 17:07
597 查看
using std::cin; using std::cout; using std::istream; using std::ostream; using std::endl; class VectorDouble { public: bool operator =(const VectorDouble&); friend bool operator ==(const VectorDouble&,const VectorDouble&); int capasity(); int size(); bool push_back(double); bool pop_back(); bool reserve(int); bool resize(int); bool empty(); double value_at(int); void change_value_at(int,double); void traval(); VectorDouble(); VectorDouble(int); VectorDouble(VectorDouble&); ~VectorDouble(); private: bool full(); double *p; int count; int max_count; }; void VectorDouble::traval() { for(int i=0;i<count;i++) { cout<<p[i]<<" "; } cout<<endl; } bool VectorDouble::operator =(const VectorDouble& temp) { if(max_count>=temp.count) { count=temp.count; max_count=temp.max_count; p=new double[max_count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } else { delete [] p; p=new double[temp.count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } return true; } bool operator ==(const VectorDouble& data1,const VectorDouble& 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; } /*******************************************/ VectorDouble::VectorDouble() { p=new double[50]; count=0; max_count=50; } VectorDouble::VectorDouble(int length) { p=new double[length]; count=0; max_count=length; } VectorDouble::VectorDouble(VectorDouble& temp) { count=temp.count; max_count=temp.max_count; p=new double[max_count]; for(int i=0;i<temp.count;i++) p[i]=temp.p[i]; } /*******************************************/ VectorDouble::~VectorDouble() { delete [] p; } /*******************************************/ bool VectorDouble::full() { if(count==max_count) return true; else return false; } bool VectorDouble::empty() { if(count==0) return true; else return false; } /*******************************************/ int VectorDouble::capasity() { return max_count; } int VectorDouble::size() { return count; } /*******************************************/ bool VectorDouble::reserve(int length) { if(length<=max_count) return false; double *temp; temp=new double[max_count]; for(int i=0;i<count;i++) { temp[i]=p[i]; } delete [] p; max_count=length; p=new double[max_count]; for(i=0;i<count;i++) { p[i]=temp[i]; } delete [] temp; return true; } bool VectorDouble::resize(int length) { if(length<=max_count) { count=length; } else if(length>max_count) { count=length; reserve(length); } return true; } bool VectorDouble::push_back(double data) { if(full()) { reserve(max_count*2); p[count++]=data; } else { p[count++]=data; } return true; } bool VectorDouble::pop_back() { if(empty()) { cout<<"VectorDouble Empty"<<endl; exit(1); } else { count--; } return true; } /*******************************************/ double VectorDouble::value_at(int i) { if(i>=count) { cout<<"Error location"<<endl; return -1; } return p[i]; } void VectorDouble::change_value_at(int i,double data) { if(i>=count) { cout<<"Error location"<<endl; } p[i]=data; } /*******************************************/
由于是一个简易的vector,所以在写的时候只假定存放double类型的值,可以写成一个模板类,效果更好哟,亲
在写这个的时候,发现vc6.0的一个惊天大bug,不能在友元函数中访问private变量,估计是命名空间的问题。
不用
using namespace std;
换成每一个小项
using std::cin; using std::cout; using std::istream; using std::ostream; using std::endl;
编译通过,测试正常
可能存在一些bug,欢迎指出 !
相关文章推荐
- c++实现一个简易vector加强版
- STL源码之实现一个简易的Vector容器
- c++实现vector类,并有一个排序和一个查找的泛型算法
- C++利用链表模板类实现一个简易队列
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
- C++实现一个简易的内存池分配器
- C++自己实现一个简单地Vector
- C++ 以传入vector的方式实现返回一个数组
- C++: 实现一个简易的银行排号叫号系统
- 用Win32 SDK & C++实现的一个简易版局域网聊天软件
- c/c++实现的一个动态分配内存的结构体数组(类似vector)
- 自己实现一个简易web服务器
- Qt学习之路(32): 一个简易画板的实现(Graphics View)
- C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
- C++实现一个web服务器, 弱智版服务器
- binder 一个简单的c++服务的实现,与callback实现
- 面试题:C++实现一个不能被继承的类
- 用C++实现HTTP服务器 - 如何控制一个套接字的发送带宽
- c++自己实现的一个程序
- c++链表实现学生成绩管理系统(简易版)