【发布时间】:2016-01-06 00:57:41
【问题描述】:
string s("Hello World!!!");// punct_cnt has the same type that s.size returns; <br>
decltype(s.size()) punct_cnt = 0;// count the number of punctuation characters in s <br>for (auto c : s) // for every char in s
if (ispunct(c)) // if the character is punctuation <br>
++punct_cnt; // increment the punctuation counter <br>
cout << punct_cnt<< " punctuation characters in " << s << endl;
这是 C++ Primer 5th 上的代码示例。
有人可以向我解释一下为什么它可以分配 0 来初始化一个不是整数类型的 string::size_t 类型的变量吗?
我在问答中找不到类似的问题。谢谢!
【问题讨论】:
-
谁告诉你
std::string::size_type不是整数类型? -
size_type => stackoverflow.com/a/4849646/1746118
-
@ForEveR 我想我见过类似 string::size_type is not int 因为 int 只能容纳 32 位但 string::size_t 可以保证容纳足够的数字。
-
@Des1gnWizard “不是
int”和不是整数类型之间有很大的区别。byte、short、int、long和long long都是整数/整数类型,但其中只有一个是int。int的大小也不是一成不变的,see the table on this page,位宽:至少 16 -
@ForEveR 哦,,,是的,我实际上想说不是“int”,但无论如何都是错误的。感谢您指出这一点!