string::size_type 而不是 int

发布于 2024-07-29 05:56:05 字数 168 浏览 7 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老街孤人 2024-08-05 05:56:05

空头也能容纳数字。 与签名字符一样。

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

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

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

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

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 any strings.

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

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.

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.

超可爱的懒熊 2024-08-05 05:56:05

您给出的示例

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

来自 Koenig 的 Accelerated C++。 随后他也说出了自己选择的理由,即:

std::string 类型将 size_type 定义为
用于保存字符串中字符数的适当类型。 每当我们需要本地人
要包含字符串大小的变量,我们应该使用 std::string::size_type 作为该变量的类型。

我们给 cols 一个 std::string::size_type 类型的原因是
确保 cols 能够包含字符数
在问候语中,无论这个数字有多大。 我们可以简单地
已经说过 cols 具有 int 类型,事实上,这样做可能会
工作。 然而,cols 的值取决于输入的大小
我们的程序,我们无法控制输入的长度。
可以想象有人可能会给我们的程序一个这么长的字符串
int 不足以包含其长度。

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:

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.

山有枢 2024-08-05 05:56:05

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

在应用程序代码中使用它是没有意义的,size_t 完全没问题(int 则不然,因为它已签名,您将收到签名/未签名的比较警告)。

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

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

会傲 2024-08-05 05:56:05

size_tstd::string::size_type 是相同的类型,除了一个重要的区别:如果它们都表示任意大小的值,std:: string::size_typestd::string 的成员)使用静态常量值 -1 来表示 npos。 超过字符串末尾的一位。 它告诉程序您已到达字符串的末尾。 如果您正在对字符串使用 searchfinderasereplace 或任何其他修改操作,或者写入如果您自己的,那么您可能更喜欢 std::string::size_type。 如果您只是迭代字符串 size_t 可能没问题。

size_t and std::string::size_type are the same types except for one important difference: Were they both will represent a value of any size, std::string::size_type (A member of std::string) uses a static constant value of -1 to represent npos. one past the end of the string. It tells the program that you have reached the end of the string. If you are using search, find, erase, replace, or any other modifying operations on strings, or writing your own then you may want to prefer std::string::size_type. If you are just iterating over the string size_t is probably fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文