使用vector和pointer的双下标重载的区别
Difference of double subscript overloading between using vector and pointer
我正在尝试使用向量和指针重载 [][]。实际上,我成功地编写了它们。但是我对矢量版本有点困惑。请参考我的实现如下:
这是指针版本:
class Array2 {
private:
unsigned row, col;
int** arr;
public:
Array2(unsigned r=0, unsigned c=0): row(r), col(c) {
if(row * col != 0) {
arr = new int*[row];
for(int i = 0; i < row; i++) {
arr[i] = new int[col];
}
}
}
class Proxy {
public:
Proxy(int* _array) : _array(_array) { }
int& operator[](int index) {
return _array[index];
}
private:
int* _array;
};
Proxy operator [] (int index) {
return Proxy(arr[index]);
}
}
这是矢量版:
class Array2 {
private:
unsigned row, col;
vector<vector<int> > arr;
public:
Array2(unsigned r=0, unsigned c=0)
: row(r), col(c), arr(r, vector<int>(c)) { }
vector<int>& operator [] (int index) {
return arr[index];
}
}
这是失败矢量版本:
class Array2 {
private:
unsigned row, col;
vector<vector<int> > arr;
public:
Array2(unsigned r=0, unsigned c=0)
: row(r), col(c), arr(r, vector<int>(c)) { }
class Proxy {
public:
Proxy(vector<int> _array) : _array(_array) { }
int& operator[](int index) {
return _array[index];
}
private:
vector<int> _array;
};
Proxy operator [] (int index) {
return Proxy(arr[index]);
}
}
使用失败版本,我无法使用 arr[2][3] = 23
等操作成功地为向量赋值。谁能告诉我我对失败向量版本的误解是什么?非常感谢。
当Proxy(vector<int> _array) : _array(_array) { }
时,vector
被复制。这意味着 Proxy
中的 _array
与原始 vector
无关(即 arr[index]
当 return Proxy(arr[index]);
时)
您可以保存指向 vector
的指针。如:
class Proxy {
public:
Proxy(vector<int>& _array) : p(&_array) {}
int& operator[](int index) {
return (*p)[index];
}
private:
vector<int>* p;
};
当然,你成功的矢量版就更好了
我正在尝试使用向量和指针重载 [][]。实际上,我成功地编写了它们。但是我对矢量版本有点困惑。请参考我的实现如下:
这是指针版本:
class Array2 {
private:
unsigned row, col;
int** arr;
public:
Array2(unsigned r=0, unsigned c=0): row(r), col(c) {
if(row * col != 0) {
arr = new int*[row];
for(int i = 0; i < row; i++) {
arr[i] = new int[col];
}
}
}
class Proxy {
public:
Proxy(int* _array) : _array(_array) { }
int& operator[](int index) {
return _array[index];
}
private:
int* _array;
};
Proxy operator [] (int index) {
return Proxy(arr[index]);
}
}
这是矢量版:
class Array2 {
private:
unsigned row, col;
vector<vector<int> > arr;
public:
Array2(unsigned r=0, unsigned c=0)
: row(r), col(c), arr(r, vector<int>(c)) { }
vector<int>& operator [] (int index) {
return arr[index];
}
}
这是失败矢量版本:
class Array2 {
private:
unsigned row, col;
vector<vector<int> > arr;
public:
Array2(unsigned r=0, unsigned c=0)
: row(r), col(c), arr(r, vector<int>(c)) { }
class Proxy {
public:
Proxy(vector<int> _array) : _array(_array) { }
int& operator[](int index) {
return _array[index];
}
private:
vector<int> _array;
};
Proxy operator [] (int index) {
return Proxy(arr[index]);
}
}
使用失败版本,我无法使用 arr[2][3] = 23
等操作成功地为向量赋值。谁能告诉我我对失败向量版本的误解是什么?非常感谢。
当Proxy(vector<int> _array) : _array(_array) { }
时,vector
被复制。这意味着 Proxy
中的 _array
与原始 vector
无关(即 arr[index]
当 return Proxy(arr[index]);
时)
您可以保存指向 vector
的指针。如:
class Proxy {
public:
Proxy(vector<int>& _array) : p(&_array) {}
int& operator[](int index) {
return (*p)[index];
}
private:
vector<int>* p;
};
当然,你成功的矢量版就更好了