C ++-您应该使用常规数组使用size_t吗? [英] C++ - should you size_t with a regular array?
问题描述
我对 size_t
感到困惑.我知道这是一个未签名的类型..对吗?我的问题是,什么时候应该使用它.是否有理由将其与常规数组一起使用?我的意思是必须声明数组的大小非常大,以至于常规的未签名或已签名的数组都无法处理.然后 size_t
就能处理好吗?有人可以给我一个例子吗?
I'm confused about size_t
. I know it's an unsigned type..right?
My question is, when should it be used. Is there a reason why it should be used with a regular array? I mean one would have to declare the array size to be really huge, so huge that a regular unsigned or signed wouldn't be able to handle it. And then a size_t
would be able to deal with it right? Can someone give me an example?
推荐答案
根据 ISO IEC 14882:2011(E)
According to ISO IEC 14882:2011(E)
§18.2 6类型size_t是实现定义的无符号整数类型,其大小足以容纳任何字节的大小.对象.
§ 18.2 6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
这使得 std :: size_t
在编写处理动态分配的库代码时成为自然选择,尤其是处理 arrays 和管理 arrays 的容器时>.
That makes std::size_t
the natural choice when writing library code that deals with dynamic allocation, particularly with arrays and containers that manage arrays.
但是在您的自己的代码中,如果您知道您的数组永远不会大于15-20个元素(例如),那么如果您更喜欢使用较小的值,则无需使用 std :: size_t
宽值.
However in your own code, if you know your array is never going to be larger than 15-20 elements (for example) then there is no need to work with std::size_t
wide values if you prefer to use something smaller.
这篇关于C ++-您应该使用常规数组使用size_t吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!