如何查找动态分配的数组的不同行的大小?

时间:2017-02-28 17:47:25

标签: c++

我有以下代码:

UnicodeDecodeError

int *exceptions[7]; int a[] = {1, 4, 11, 13}; int b[] = {5, 6, 11, 12, 14, 15}; int c[] = {2, 12, 14, 15}; int d[] = {1, 4, 7, 9, 10, 15}; int e[] = {1, 3, 4, 5, 7, 9}; int f[] = {1, 2, 3, 7, 13}; int g[] = {0, 1, 7, 12}; exceptions[0] = a; exceptions[1] = b; exceptions[2] = c; exceptions[3] = d; exceptions[4] = e; exceptions[5] = f; exceptions[6] = g; exception[0]的尺寸应分别为exception[1]4

这是我的代码:

6

但我每行都会得到short size = sizeof(exceptions[1]) / sizeof(exceptions[1][0]); 。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

 short size = sizeof(exceptions[1]) / sizeof(exceptions[1][0]);

实际上和

一样
 short size = sizeof(int*) / sizeof(int);

在64位平台上,最有可能产生2

  

我该如何解决这个问题?

使用某些c ++标准容器,例如std::vector<std::vector<int>>

std::vector<std::vector<int>> exceptions {
    {1, 4, 11, 13},
    {5, 6, 11, 12, 14, 15},
    {2, 12, 14, 15},
    {1, 4, 7, 9, 10, 15},
    {1, 3, 4, 5, 7, 9},
    {1, 2, 3, 7, 13},
    {0, 1, 7, 12},
}

您的陈述将成为:

 short size = exceptions[0].size();
 size = exceptions[1].size();

(无论需要什么)

答案 1 :(得分:0)

最好的补救措施是使用标准模板库中提供的向量。它们有一个你可以使用的size()函数,它们比数组更通用。