为什么我的int输出的东西与应该的东西不同?

发布于 2025-02-13 06:27:55 字数 1490 浏览 2 评论 0原文

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

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

发布评论

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

评论(1

小草泠泠 2025-02-20 06:27:56

您只将a转换为int,您就不会使用法律转换器(从<代码> std :: string 无法正常工作,如果您的编译器不警告您,我会感到惊讶),然后将分配给结果(> =),而不是比较==)(也希望编译器警告您)。

后来检查了这些警告:当发生时,它看起来像gcc在串联中接受两个构造是完全不同的构造(IT解释if(int(a)) = 1) as 声明阴影int变量名为a,初始化为1,该>到期之后/ 链条,它仅在使用-wall进行编译时才警告,即使到那时,它也只会抱怨,因为它认为它应该是,如果(int a = 1)将其制作名称无外括号的阴影变量,ugh。光明的一面是,将分配固定在比较中会揭示另一个问题。这是始终在警告中始终编译的一个很好的理由(-wextra有时过于健谈,但是-wall几乎总是指出真正的问题),可悲的是,这是一个相当间接的在这种情况下警告。

先前转换一次带有有效的string to <<代码> int 转换器如std :: stoi ,然后测试转换的intint s,而不是std :: String反对int,例如:

const int aint = std::stoi(a);
if (aint == 1)  // Using ==, not =
{
    bin = 60457811425;
}
else if (aint == 2)  // Testing int == int, not string == int
{
    bin = 60457811474;
}
else if (aint == 3)  // Testing int == int, not string == int
{
    bin = 6045781165;
}

或对于更简洁的代码(也没有额外的命名变量):

switch(std::stoi(a)) {
    case 1: bin = 60457811425; break;
    case 2: bin = 60457811474; break;
    case 3: bin = 6045781165; break;
    /* Maybe put a default case here to handle invalid input? */
}

You only converted to a to an int once, you didn't do it with a legal converter ("constructing" an int from a std::string doesn't work like that, and I'd be surprised if your compiler didn't warn you), and you assigned to the result (=) rather than comparing (==) (also something I'd expect a compiler to warn you about).

Later checked about those warnings: As it happens, it looks like gcc accepts both constructs in tandem as a completely different construct (it interprets if (int(a) = 1) as declaring a shadowing int variable named a, initialized to 1, that expires after the if/else if chain; that's fun). It only warns at all if you compile with -Wall, and even then, it only complains because it thinks it should be if (int a = 1) to make the name shadowing variable without extraneous parentheses, ugh. Bright side is that fixing the assignment to a comparison would then reveal the other problem. That's a good reason to always compile with warnings turned up (-Wextra is sometimes overly chatty, but -Wall is almost always pointing out real problems), sadly it's a rather indirect warning in this case.

Convert it once up front with a valid string to int converter like std::stoi, and then test the converted int value against other ints, not std::string against int, e.g.:

const int aint = std::stoi(a);
if (aint == 1)  // Using ==, not =
{
    bin = 60457811425;
}
else if (aint == 2)  // Testing int == int, not string == int
{
    bin = 60457811474;
}
else if (aint == 3)  // Testing int == int, not string == int
{
    bin = 6045781165;
}

Or for slightly more succinct code (and no extra named variables):

switch(std::stoi(a)) {
    case 1: bin = 60457811425; break;
    case 2: bin = 60457811474; break;
    case 3: bin = 6045781165; break;
    /* Maybe put a default case here to handle invalid input? */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文