gpt4 book ai didi

c++ - 如何使用 sizeof(a)/sizeof(a[n])

转载 作者:行者123 更新时间:2023-11-28 00:07:57 28 4
gpt4 key购买 nike

To avoid things quietly breaking if you change the array size, I suggest std::copy(a, a + sizeof(a)/sizeof(a[0]), b);. Even better, wrap the sizeof() junk in a macro -- or even betterer, use a function template instead: template <typename T, size_t N> size_t size((T&)[N]) { return N; } – j_random_hacker Sep 8 '12 at 7:29

当我今天早上查看问答时,我发现了这条评论(有 4 票赞成)。我对 C++ 很陌生。什么是a+sizeof(a[0])意思是这里,我想sizeof(a[0])将返回 4 代表一个 int 内存字节?非常感谢!!

最佳答案

这很简单:

sizeof(a)/sizeof(a[0])

无论您选择哪个元素(ie n0)

关于c++ - 如何使用 sizeof(a)/sizeof(a[n]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476676/

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