【发布时间】:2020-05-30 02:13:31
【问题描述】:
我需要你的帮助。 我的功能是:(它的工作方式是真的)
#include <iostream>
using namespace std;
#define V 4
#define INF 999
int floydWarshall(int graph[][V]){
int dist[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
return dist[V][V];
}
这行有错误数组必须用大括号括起来的初始化器初始化:
int dist[V][V] = floydWarshall(graph[][V]);
【问题讨论】:
-
return dist[V][V]访问越界 -
如果你使用 C++ 数组而不是 C 数组,这些东西会简单 1000 倍
-
在 C++ 中尽量避免使用
#define作为常量,而是使用const int V = 4;之类的东西。这包括重要的类型信息。 -
提示:使用
std::vector作为数据容器并模拟二维结构。除非万不得已,否则不要乱用 C 数组。
标签: c++