gpt4 book ai didi

c++ - 将未知大小的二维数组传递给函数 C++

转载 作者:太空狗 更新时间:2023-10-29 23:00:13 25 4
gpt4 key购买 nike

基本上,我的问题是:我让用户定义二维数组的大小 (N,M),然后我声明:

整数矩阵[N][M];

然后我需要将这个未初始化的矩阵传递给一个函数,该函数从 .csv 文件中读取一些数据并将其放入矩阵中,所以我尝试了:

void readwrite(int &matrix[N][], const int N, const int M){....};


int main(){
....
cin>>N;
cin>>M;

int matrix[N][M];
readwrite(matrix,N,M);
};

但是,当我编译它时,出现以下错误:“N 未在此范围内声明”。

关于如何使这项工作有任何想法吗?

谢谢大家!

最佳答案

OP 正在尝试的东西很难正确实现,而且实现它的好处与成本相比是如此微不足道......好吧,我将引用经典。

The only winning move is not to play.

-约书亚, war 游戏

您不能安全地将 C++ 中动态分配的二维数组传递给函数,因为您始终必须在编译时至少知道一个维度。

我可以指向 Passing a 2D array to a C++ function因为那看起来像一个很好的拷贝。我不会,因为它指的是静态分配的数组。

您可以玩一些愚蠢的转换游戏,强制将数组放入函数中,然后再将其转换回内部。我不打算解释如何做到这一点,因为它是史诗级的愚蠢行为,应该是一种开火进攻。

你可以传递一个指向指针的指针,int **,但是构造和销毁逻辑是一组怪诞的new和循环。此外,最终结果将分配的内存分散在 RAM 周围,削弱了处理器在预测和缓存方面的尝试。在现代处理器上,如果您无法预测和缓存,您就会放弃 CPU 的大部分性能。

您要做的是保持一维。一维数组很容易通过。索引算法非常简单且易于预测。都是一个内存块,因此缓存命中的可能性更大。

制作一维数组很简单:不需要。使用 std::vector相反。

std::vector<int> arr(rows*columns);

如果你必须这样做,因为作业规范说“没有 vector !”好吧,你被困住了。

int * arr = new int[rows*columns];

注意我使用的是,而不是MN。当遇到 MN 哪个是哪个?谁知道,谁在乎,为什么首先要对自己这样做?给你的变量起一个好的、描述性的名字,并享受在你以后调试代码时能够阅读你的代码所节省的时间。

用法的核心与数组和 vector 相同:

 int test = arr[row * columns + column];

将在 [row][column] 处恢复二维空间中的元素。我不应该解释这些变量的含义。 MN 死亡。

定义一个函数是:

void function (std::vector<int> & arr, size_t rows, size_t columns) 

或者(恶心)

void function (int * arr, size_t rows, size_t columns) 

请注意, 的类型为size_tsize_t 是无符号的(负数组大小不是您想要的,那么为什么允许它呢?)并且保证它足够大以容纳您可以使用的最大可能数组索引。换句话说,它比 int 更合适。但为什么要到处传递 rowscolumns 呢?在这一点上,明智的做法是围绕数组及其控制变量创建一个包装器,然后绑定(bind)一些函数以使其更易于使用。

template<class TYPE>
class Matrix
{
private:
size_t rows, columns;
std::vector<TYPE> matrix;
public:
// no default constructor. Matrix is BORN ready.

Matrix(size_t numrows, size_t numcols):
rows(numrows), columns(numcols), matrix(rows * columns)
{
}
// vector handles the Rule of Three for you. Don't need copy and move constructors
// a destructor or assignment and move operators

// element accessor function
TYPE & operator()(size_t row, size_t column)
{
// check bounds here
return matrix[row * columns + column];
}

// constant element accessor function
TYPE operator()(size_t row, size_t column) const
{
// check bounds here
return matrix[row * columns + column];
}

// stupid little getter functions in case you need to know how big the matrix is
size_t getRows() const
{
return rows;
}
size_t getColumns() const
{
return columns;
}

// and a handy-dandy stream output function
friend std::ostream & operator<<(std::ostream & out, const Matrix & in)
{
for (int i = 0; i < in.getRows(); i++)
{
for (int j = 0; j < in.getColumns(); j++)
{
out << in(i,j) << ' ';
}
out << '\n';
}

return out;
}

};

对数组版本的粗略描述只是为了展示允许 vector 完成其工作的好处。未经测试。可能包含咆哮。重点是更多的代码和更多的错误空间。

template<class TYPE>
class ArrayMatrix
{
private:
size_t rows, columns;
TYPE * matrix;
public:
ArrayMatrix(size_t numrows, size_t numcols):
rows(numrows), columns(numcols), matrix(new TYPE[rows * columns])
{
}

// Array version needs the copy and move constructors to deal with that damn pointer
ArrayMatrix(const ArrayMatrix & source):
rows(source.rows), columns(source.columns), matrix(new TYPE[rows * columns])
{
for (size_t i = 0; i < rows * columns; i++)
{
matrix[i] = source.matrix[i];
}
}

ArrayMatrix(ArrayMatrix && source):
rows(source.rows), columns(source.columns), matrix(source.matrix)
{
source.rows = 0;
source.columns = 0;
source.matrix = nullptr;
}

// and it also needs a destructor
~ArrayMatrix()
{
delete[] matrix;
}

TYPE & operator()(size_t row, size_t column)
{
// check bounds here
return matrix[row * columns + column];
}

TYPE operator()(size_t row, size_t column) const
{
// check bounds here
return matrix[row * columns + column];
}

// and also needs assignment and move operator
ArrayMatrix<TYPE> & operator=(const ArrayMatrix &source)
{
ArrayMatrix temp(source);
swap(*this, temp); // copy and swap idiom. Read link below.
// not following it exactly because operator=(ArrayMatrix source)
// collides with operator=(ArrayMatrix && source) of move operator
return *this;
}

ArrayMatrix<TYPE> & operator=(ArrayMatrix && source)
{
delete[] matrix;
rows = source.rows;
columns = source.columns;
matrix = source.matrix;

source.rows = 0;
source.columns = 0;
source.matrix = nullptr;

return *this;
}

size_t getRows() const
{
return rows;
}
size_t getColumns() const
{
return columns;
}
friend std::ostream & operator<<(std::ostream & out, const ArrayMatrix & in)
{
for (int i = 0; i < in.getRows(); i++)
{
for (int j = 0; j < in.getColumns(); j++)
{
out << in(i,j) << ' ';
}
out << std::endl;
}

return out;
}

//helper for swap.
friend void swap(ArrayMatrix& first, ArrayMatrix& second)
{
std::swap(first.rows, second.rows);
std::swap(first.columns, second.columns);
std::swap(first.matrix, second.matrix);
}

};

创建其中之一是

Matrix<int> arr(rows, columns);

现在传递数组是

void func(Matrix & arr);

使用数组是

int test = arr(row, column);

所有索引数学都隐藏在视线之外。

其他引用:

What is the copy-and-swap idiom?

What is The Rule of Three?

关于c++ - 将未知大小的二维数组传递给函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34277827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com