简体   繁体   English

如何将向量的向量从类传递到函数而不进行复制?

[英]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 . 这里有rowscols的单独问题。 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 您可以重写GetRowsGetCols (无论如何应为const)以委托给向量的大小,并且不具有Matrix存储rowscols数据成员

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);来更改vm.GetPointer().resize(N); your current GetRows and GetCols would be wrong! 您当前的GetRowsGetCols是错误的!

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.

相关问题 来自矢量矢量的OpenCV矩阵,无需复制数据 - OpenCV matrix from vector of vectors without copying the data 从融合向量的std向量返回列,但不进行复制 - returning column from a std vector of fusion vectors, without copying 如何从类中返回大向量而不复制数据 - How to return large vector from class without copying data 如何传递只读向量 <int> 成为对向量的一部分的函数 <int,int> 没有复制 - How to pass a read-only vector<int> to a function which is part of a vector of pair<int,int> without copying 如何将向量从类传递给函数? - How to pass vector from a class to a function? 在向量的向量中访问C ++向量而不复制它 - Accessing a C++ vector in a vector of vectors without copying it 在课堂上传递矢量而不复制 - Passing vector in class without copying 如何将向量的向量传递到期望指向指针的(原始)指针的函数中 - how to pass a vector of vectors into a function expecting a (raw) pointer to pointers 如何将向量从成员函数传递到同一类中的另一个函数? - How to pass a vector from a member function to another in the same class? 如何在std :: unordered_map中更新向量 <std::string, std::vector<int> &gt;没有复制? - How to update the vectors in a std::unordered_map<std::string, std::vector<int>> without copying?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM