为什么我需要在c++中使用‘size_t’?

时间:2022-05-27 17:07:08

As a beginner, I'm really confused about size_t. I can use int, float or other types. Why still declare size_t type. I don't feel its advantages. I've viewed some pages, but I still can't understand it.

作为一个初学者,我对size_t很困惑。我可以使用int、float或其他类型。为什么还要声明size_t类型。我不觉得它有什么好处。我看了几页,还是看不懂。

3 个解决方案

#1


17  

Its main advantage is that it's the right tool for the job.

它的主要优点是它是适合这项工作的工具。

size_t is literally defined to be big enough to represent the size of any object on your platform. The others are not. So, when you want to store the size of an object, why would you use anything else?

size_t从字面上定义为足以表示平台上任何对象的大小。其他人不是。所以,当你想要存储一个对象的大小时,你为什么要使用其他东西呢?

You can use int if you like, but you'll be deliberately choosing the inferior option that leads to bugs. I don't quite understand why you'd want to do so, but hey it's your code.

如果您愿意,可以使用int,但是您将故意选择导致错误的劣质选项。我不太明白你为什么要这么做,但这是你的代码。

If you choose to use float, though, please tell us what program you're writing so we can avoid it. :)

如果您选择使用float,请告诉我们您正在编写什么程序,这样我们就可以避免使用它。:)

#2


4  

Using a float would be horrible since that would be a misuse of floating point types, plus type promotion would mean that multiplying the size of anything would take place in floating point!

使用浮点数是很可怕的,因为这将是浮点类型的误用,加上类型升级将意味着在浮点数中增加任何东西的大小!

Using a int would also be horrible since the specifics of an int are intentionally loosely defined by the C++ standard. (It could be as small as 16 bits).

使用int也很可怕,因为c++标准有意地松散地定义了int的细节。(它可能只有16位那么小)。

But a size_t type is guaranteed to adequately represent the size of pretty much anything and certainly the sizes of containers in the C++ standard library. Its specific details are dependent on a particular platform and architecture. The fact that it's an unsigned type is the subject of much debate. (I personally believe it was a mistake to make it unsigned as it can mess up code using relational operators and introduce pernicious bugs that are difficult to spot).

但是,size_t类型被保证能够充分表示几乎所有东西的大小,当然也可以表示c++标准库中容器的大小。它的具体细节取决于特定的平台和体系结构。它是一种无符号类型这一事实引起了很多争论。(我个人认为这是一个错误,因为它会破坏代码,使用关系操作符,并引入难以识别的有害bug)。

#3


0  

I would advise you to use size_t whenever you want to store sizes of classes or structures or when you deal with raw memory(e.g. storing size of raw memory or using as an index of a raw array). However for indexing/iterating over standard containers (such as std::vector), I recommend using underlying size type of a given container(e.g. vector::size_type).

我建议您在存储类或结构的大小或处理原始内存时使用size_t。存储原始内存的大小或用作原始数组的索引)。但是,对于对标准容器(如std::vector)进行索引/迭代,我建议使用给定容器的底层大小类型(例如。向量:size_type)。

#1


17  

Its main advantage is that it's the right tool for the job.

它的主要优点是它是适合这项工作的工具。

size_t is literally defined to be big enough to represent the size of any object on your platform. The others are not. So, when you want to store the size of an object, why would you use anything else?

size_t从字面上定义为足以表示平台上任何对象的大小。其他人不是。所以,当你想要存储一个对象的大小时,你为什么要使用其他东西呢?

You can use int if you like, but you'll be deliberately choosing the inferior option that leads to bugs. I don't quite understand why you'd want to do so, but hey it's your code.

如果您愿意,可以使用int,但是您将故意选择导致错误的劣质选项。我不太明白你为什么要这么做,但这是你的代码。

If you choose to use float, though, please tell us what program you're writing so we can avoid it. :)

如果您选择使用float,请告诉我们您正在编写什么程序,这样我们就可以避免使用它。:)

#2


4  

Using a float would be horrible since that would be a misuse of floating point types, plus type promotion would mean that multiplying the size of anything would take place in floating point!

使用浮点数是很可怕的,因为这将是浮点类型的误用,加上类型升级将意味着在浮点数中增加任何东西的大小!

Using a int would also be horrible since the specifics of an int are intentionally loosely defined by the C++ standard. (It could be as small as 16 bits).

使用int也很可怕,因为c++标准有意地松散地定义了int的细节。(它可能只有16位那么小)。

But a size_t type is guaranteed to adequately represent the size of pretty much anything and certainly the sizes of containers in the C++ standard library. Its specific details are dependent on a particular platform and architecture. The fact that it's an unsigned type is the subject of much debate. (I personally believe it was a mistake to make it unsigned as it can mess up code using relational operators and introduce pernicious bugs that are difficult to spot).

但是,size_t类型被保证能够充分表示几乎所有东西的大小,当然也可以表示c++标准库中容器的大小。它的具体细节取决于特定的平台和体系结构。它是一种无符号类型这一事实引起了很多争论。(我个人认为这是一个错误,因为它会破坏代码,使用关系操作符,并引入难以识别的有害bug)。

#3


0  

I would advise you to use size_t whenever you want to store sizes of classes or structures or when you deal with raw memory(e.g. storing size of raw memory or using as an index of a raw array). However for indexing/iterating over standard containers (such as std::vector), I recommend using underlying size type of a given container(e.g. vector::size_type).

我建议您在存储类或结构的大小或处理原始内存时使用size_t。存储原始内存的大小或用作原始数组的索引)。但是,对于对标准容器(如std::vector)进行索引/迭代,我建议使用给定容器的底层大小类型(例如。向量:size_type)。