C++ string::size_type 而不是 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1181079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:06:12  来源:igfitidea点击:

string::size_type instead of int

c++stringintsize-type

提问by Delirium tremens

const std::string::size_type cols = greeting.size() + pad * 2 + 2;

Why string::size_type? intis supposed to work! it holds numbers!!!

为什么string::size_typeint应该可以工作!它有数字!!!

回答by jalf

A short holds numbers too. As does a signed char.

短也包含数字。签名字符也是如此。

But none of those types are guaranteed to be large enough to represent the sizes of anystrings.

但是这些类型都不能保证足够大以表示任何字符串的大小。

string::size_typeguarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is.

string::size_type保证就是这样。它是一种足以表示字符串大小的类型,无论该字符串有多大。

For a simple example of why this is necessary, consider 64-bit platforms. An int is typically still 32 bit on those, but you have far more than 2^32 bytes of memory.

有关为什么需要这样做的简单示例,请考虑 64 位平台。一个 int 通常仍然是 32 位的,但你有远远超过 2^32 字节的内存。

So if a (signed) int was used, you'd be unable to create strings larger than 2^31 characters. size_type will be a 64-bit value on those platforms however, so it can represent larger strings without a problem.

因此,如果使用(有符号的)int,您将无法创建大于 2^31 个字符的字符串。但是,size_type 在这些平台上将是 64 位值,因此它可以毫无问题地表示更大的字符串。

回答by mihai

The example that you've given,

你举的例子,

const std::string::size_type cols = greeting.size() + pad * 2 + 2;

is from Accelerated C++ by Koenig. He also states the reason for his choice right after this, namely:

来自Koenig 的 Accelerated C++。在此之后,他还说明了他选择的原因,即:

The std::string type defines size_type to be the name of the appropriate type for holding the number of characters in a string. Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable.

The reason that we have given cols a type of std::string::size_type is to ensure that cols is capable of containing the number of characters in greeting, no matter how large that number might be. We could simply have said that cols has type int, and indeed, doing so would probably work. However, the value of cols depends on the size of the input to our program, and we have no control over how long that input might be. It is conceivable that someone might give our program a string so long that an int is insufficient to contain its length.

std::string 类型将 size_type 定义为用于保存字符串中字符数的适当类型的名称。每当我们需要一个局部变量来包含字符串的大小时,我们应该使用 std::string::size_type 作为该变量的类型。

我们给 cols 一个 std::string::size_type 类型的原因是为了确保 cols 能够包含问候语中的字符数,无论这个数字有多大。我们可以简单地说 cols 具有 int 类型,实际上,这样做可能会奏效。但是,cols 的值取决于程序输入的大小,我们无法控制输入的长度。可以想象,有人可能会给我们的程序一个太长的字符串,以至于 int 不足以包含它的长度。

回答by Marc Mutz - mmutz

A nested size_typetypedef is a requirement for STL-compatible containers (which std::stringhappens to be), so generic code can choose the correct integer type to represent sizes.

嵌套size_typetypedef 是 STL 兼容容器(std::string恰好是)的要求,因此通用代码可以选择正确的整数类型来表示大小。

There's no point in using it in application code, a size_tis completely ok (intis not, because it's signed, and you'll get signed/unsigned comparison warnings).

在应用程序代码中使用它是没有意义的,asize_t是完全可以的(int不是,因为它是已签名的,并且您将收到已签名/未签名的比较警告)。