gpt4 book ai didi

c++ - 如何找到动态分配数组的不同行的大小?

转载 作者:行者123 更新时间:2023-11-30 02:26:36 24 4
gpt4 key购买 nike

我有以下代码:

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] 的大小应分别为 46

这是我的代码:

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

但我得到的每一行都是 2。我该如何解决这个问题?

最佳答案

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

实际上和

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

在 64 位平台上,这很可能产生 2 .


How can I solve this problem?

使用一些 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();

(无论需要什么)

关于c++ - 如何找到动态分配数组的不同行的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42515494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com