这个Resize()成员函数该怎么定义才能调整向量的个数啊
#include<iostream>using namespace std;
class vector
{
private:
int* buffer;
int _size;
public:
vector(int size = 10) : _size(size)
{
buffer = new int[size];
}
~vector()
{
delete[] buffer;
}
vector(const vector& other)
: _size(other._size)
{
buffer = new int[other._size];
for (int i = 0; i < _size; i++)
buffer[i] = other.buffer[i];
}
Resize()//成员函数
{
}
int& operator[] (int index)
{
if (index >= _size)
{
int* data = new int[index + 10];
for (int i = 0; i < _size; i++)
data[i] = buffer[i];
delete[] buffer;
buffer = data;
_size = index + 10;
}
return buffer[index];
}
};
void main()
{
vector a(10);
for(int i=0;i<10;i++)a[i]=i+1;
a.Resize(20);
for(i=0;i<20;i++)cout<<a[i]<<endl;
}