cpp大作业,模仿cpp stl里的vector,编写一个PointVector类存储,Point类的数据,参考c++reference里vector的描述完成。
先上代码:
#include<iostream>
using namespace std;
struct Point {
Point(int x = 0, int y = 0) :x(x), y(y) {}
void print() {
cout << "(" << x << "," << y << ")";
}
int x;
int y;
};
class PointVector {
private:
Point* data;
unsigned size_;
unsigned capacity_;
public:
static Point NULLPoint;//返回Point操作失败时返回的空Point
//creators
PointVector() :size_(0), capacity_(0) { data = new Point[capacity_]; }
PointVector(const PointVector& x):size_(x.size_),capacity_(x.capacity_){
data = new Point[x.capacity_];
for (unsigned i = 0; i < size_; i++) {
data[i] = x.data[i];
}
}
//destructor
~PointVector() {
delete[]data;
}
//重载运算符
PointVector& operator=(const PointVector& pv) {
delete[]data;
size_ = pv.size_;
capacity_ = pv.capacity_;
data = new Point[pv.capacity_];
for (unsigned i = 0; i < capacity_; i++) {
data[i] = pv.data[i];
}
return *this;
}
PointVector& operator+(const PointVector