【发布时间】:2018-12-17 15:35:36
【问题描述】:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s("092282");
cout << s[0];
if (s[0] < (char)9)
{
cout << "yesss";
}
}
在本文中,我无法理解如何比较数字常量和数字的字符串元素。
【问题讨论】:
-
你希望 '(char)9' 做什么?使用 cast 运算符不会达到您的预期
-
@kiner_shah:或者简单地说:
if (s[0] < '9')? -
旁注:小心你的
#include <bits/stdc++.h>。 -
@JVApen 对于
!等字符返回true。你应该改用std::isdigit。 -
@LightnessRacesinOrbit:同意,但是,在某些平台上,这不是像
s[0] - '0'这样的未定义行为。