我想从文件中读取Graph并启动函数以获得最大流量,但是当我将此图作为函数参数传递时,我得到一些错误。我做错了什么?
int main() {
fstream file;
file.open( "macierz.txt", ios::in );
int n = 7;
int graph[n][n];
int v;
for(int i = 0; i < n; i++){
for(int j = 0 ; j < n; j++){
file >> v;
graph[i][j] = v;
}
}
cout << "Ford-Fulkerson -MATRIX- The maximum possible flow: " << fordFulkersonMatrix(graph, 0, 6) << endl;
功能:
int fordFulkersonMatrix(int graph[7][7], int start, int target) {
int u, v;
int rGraph[7][7];
int parent[7];
int max_flow = 0;
for (u = 0; u < 7; u++) {
for (v = 0; v < 7; v++)
{
rGraph[u][v] = graph[u][v];
}
}
错误:
main.cpp|200|error: cannot convert 'int (*)[(((unsigned int)(((int)n) + -0x000000001)) + 1)]' to 'const int (*)[7]' for argument '1' to 'int fordFulkersonMatrix(const int (*)[7], int, int)'|
答案 0 :(得分:0)
如果你用Java编程,肯定你会喜欢C ++ STL接口:
// or even better - use Eigen as Peter K mentioned
using MyCustomMatrix = std::vector<std::vector<int> >;
// input by const ref to prevent std::vector copy
int fordFulkersonMatrix(const MyCustomMatrix& input, int start, int target) {
// do whatever you need to do
// you can access your matrix elements like this:
int elem34 = input[3][4]; // only if your matrix if big enough, of course
// you can iterare using C++11 range loops
for (const auto& row : input) {
for(auto elem : row) {
std::cout << "My elem: " << elem << std::endl;
}
}
// and you can access your matrix dim information like this:
int numRows = input.size();
if (numRows > 0) {
int numCols = input[0].size();
}
return 0;
}
稍后在代码中,您可以像这样构建矩阵:
MyCustomMatrix mat;
mat.push_back(MyCustomMatrix::value_type()); // add one row
mat[0].push_back(69); // add one elem to the first row
fordFulkersonMatrix(mat, 0, 6);
预期产出:
My elem: 69
这当然是非常懒惰的解决方案,但也许你不需要更多。对于一些严肃的线性代数运算,考虑使用专门的库(如已经提到的Eigen,或者提升uBLAS)。