对于声明这种数组感到困惑

问题描述 投票:0回答:3

我正在读这个code

有一条线:pair <int, int> approach[1 << 18][17]

我不知道这个宣言的含义是什么:approach[ 1<<18 ][17];

有人可以帮忙吗?

c++ arrays std-pair
3个回答
3
投票

在这种情况下,<<是位左移位运算符。 1 << 18表示取1的二进制表示并将其向左移18.这是218(2到18的幂,或262144)。所以你有一个非常大的二维数组对:

pair <int, int> approach[262144][17];

2
投票

<<是位左移位运算符。

所以1 << 18是一个整数常量,其值为218。


1
投票

它只是意味着2 ^ 18,2为18的幂。

代码缺少一些解释,唯一真正的好信息是

// SGU 502 -- Digits Permutation

啊它的数字排列,所以

pair <int, int> approach[1 << 18][17]

可能会用来存储排列,除非对排列有一些限制,排列的数量应该是N! (希望N! <=(1 << 18))。

但是这个定义并没有说明这一点,让我们看看我们是否可以让它更清晰(希望是正确的)。

const int maxLength = 17;
const int maxPermutation = 1 << (maxLength+1);
pair <int, int> approach[maxPermutation ][maxLength]
static_assert(factorial(maxLength) <= maxPermutation, "approach might not be able to hold all permutations");
© www.soinside.com 2019 - 2024. All rights reserved.