带有索引2D数组的逗号运算符 [英] Comma Operator with indexing 2D arrays

查看:106
本文介绍了带有索引2D数组的逗号运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个算法,是图论dijkstra算法的伪代码.发生的第一件事是基本的for循环.

I have this algorithm that is pseudocode for the dijkstra algorithm for graph theory. The first thing that goes on is a basic for loop.

visitedSet[0] = true //visitedSet is a array of bools
for (int i = 1; i <= numberNodes; ++i)
{
    distanceArray[i] = adjacencyMatrix[0,i];
    //distanceArray is 1D with size of fifty
    //adjacencyMatrix is 2D with size of fifty
    //Both arrays hold values of unsigned ints
}

这是数组定义

enum GraphLimit300 {MAX_NODES = 50};
unsigned int adjacencyMatrix[MAX_NODES][MAX_NODES];
unsigned int distanceArray[MAX_NODES];

Visual Studio给我一个数组,说我不能将一个无符号整数数组分配给一个指针.我在线上查询了一下,基本上是在这种情况下使用逗号运算符抛出了第一种情况0,并将其视为distanceArray[i] = adjacencyMatrix[i];,这对我来说没有意义,因为adjacenyMatrix是2D数组.我只是想知道是什么导致了此编译错误,并获得了有关原因的更多信息,因为我基本上只是复制伪代码基本上表示的变量名.

Visual studio is giving me an array saying that I can't assign an array of unsigned integers to a pointer. I have looked up online that with the comma operator basically in this case throws out the first case, 0, and treats it as distanceArray[i] = adjacencyMatrix[i]; Which doesn't make sense to me since adjacenyMatrix is a 2D array. I am just wondering what is giving me this compile error and get more information on why, because I basically just copying in variable names where the pseudo code basically says to.

伪代码:

    S = { 1 }
for ( index = 2; index <= N; ++ index )
    D[ index ] = C[ 1, index ]
for ( index = 1; index <= N – 1; ++ index )
    Choose a vertex v in V – S such that D[ v ] is a minimum
    Add v to S
    for each vertex w in V – S do
        D[ w ] = min( D[ w ], D[ v ] + C[ v, w ] )

上面的伪代码使用列表表示它们的数组,出于某种原因,它们从1开始,因此我在代码中将其修改为从0开始.

The above pseudo code uses lists to represent their arrays they for some reason start at 1 so I modified it to start at 0 in my code.

推荐答案

您必须查看如何访问2D数组的元素.另外,请查看逗号运算符的功能.您必须两次使用[]:

You have to review how to access elements of 2D array. Also, take look at what comma operator does. You have to use [] twice:

adjacencyMatrix[0][i]

以下内容:

adjacencyMatrix[0, i]

等效于:

adjacencyMatrix[i]

还有哪些会留给您一维数组.并且,如错误消息所示:

Which will still leave you with 1D array. And, as the error message says:

   distanceArray[i] = adjacencyMatrix[i];
// ^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^
//   unsigned int   array of unsigned ints

您不可能指望这项工作会发生.

You can not possibly expect this assignment to happen.

这篇关于带有索引2D数组的逗号运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆