[英]How to pass a vector of vectors from class to function without copying?
I wrote this program where I need to transfer a vector<vector<int>>
from a class to a function to change every 0
in the vector to a 1
. 我编写了该程序,需要将vector<vector<int>>
从类转移到函数,以将vector中的每个0
更改为1
。 This function works, but somewhere a copy of vector is created and the elements are only changed to 0
in the copy, not in the original vector. 此函数有效,但是在某个地方创建了vector的副本,并且元素在副本中仅更改为0
,而在原始vector中没有更改。 How can I fix this problem? 我该如何解决这个问题?
class Matrix {
friend istream& operator >> (istream &in, IntArr& a) {...}
friend ostream& operator << (ostream& out, Matrix& a) {...}
private:
vector < vector <int> > v;
int rows, cols;
public:
Matrix(int rows, int cols) {
this->rows = rows;
this->cols = cols;
v.resize(rows, vector<int>(cols));
}
// ...
vector < vector <int> > GetPointer() {
return v;
}
int GetRows() {
return rows;
}
int GetCols() {
return cols;
}
};
void IndividualTask(int rows, int cols, vector < vector <int> > v) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (v[i][j] == 0) {
v[i][j] = 1;
}
}
}
}
int main() {
Matrix arr1(2, 2);
cin >> arr1;
IndividualTask(arr1.GetRows(), arr1.GetCols(), arr1.GetPointer());
cout << arr1;
return 0;
}
There are a couple of related problems you absolutely need to solve first. 您必须首先解决几个相关问题。 GetPointer
returns a copy, and IndividualTask
takes a copy. GetPointer
返回一个副本, IndividualTask
获取一个副本。 You'll want both of these to work by adding &
to the type to make them references 您希望通过在类型中添加&
使其成为引用来使它们都起作用
First, have GetPointer
return a reference. 首先,让GetPointer
返回引用。
// right here---+
// |
// v
vector < vector <int> >& GetPointer() {
return v;
}
Second, have IndividualTask
take a reference 第二,请IndividualTask
参考
// and again here------+
// |
// v
void IndividualTask(int rows, int cols, vector < vector <int> >& v) {
// ...
}
There is a separate problem here, with the rows
and cols
. 这里有rows
和cols
的单独问题。 A std::vector
already knows its size, this isn't C, you don't need to pass around array sizes like this. 一个std::vector
已经知道它的大小,这不是C,您不需要像这样传递数组大小。
You could rewrite GetRows
and GetCols
(which should be const anyway) to delegate to the vector's size, and not have the Matrix
store rows
and cols
data members 您可以重写GetRows
和GetCols
(无论如何应为const)以委托给向量的大小,并且不具有Matrix
存储rows
和cols
数据成员
int GetRows() const {
return v.size();
}
int GetCols() const {
// guard against the empty vector case
if (v.empty()) { return 0; }
// only correct if all rows have the same number of columns
return return v[0].size();
}
This is more than just convenience, it's correct. 这不仅是方便,这是正确的。 If someone were to change the size of your v
by doing m.GetPointer().resize(N);
如果有人通过执行m.GetPointer().resize(N);
来更改v
的m.GetPointer().resize(N);
your current GetRows
and GetCols
would be wrong! 您当前的GetRows
和GetCols
是错误的!
Your function could also use these qualities of the vector, 您的函数还可以使用矢量的这些品质,
void IndividualTask(vector < vector <int> >& v) {
for (std::size_t i = 0; i < v.size(); i++) {
for (std::size_t j = 0; j < v[i].size(); j++) {
if (v[i][j] == 0) {
v[i][j] = 1;
}
}
}
}
If you are using C++11
or alter, you can use a ranged for loop more easily 如果您使用的是C++11
或alter,则可以更轻松地使用带范围的for循环
void IndividualTask(vector < vector <int> >& v) {
for (auto& row : v) {
for (auto& e : row) {
if (e == 0) {
e = 1;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.