相关疑难解决方法(0)

C++将变量与多个值进行比较的最有效方法?

在我的程序中,有几次我必须检查变量是否是众多选项之一.例如

if (num = (<1 or 2 or 3>)) { DO STUFF }
Run Code Online (Sandbox Code Playgroud)

我已经把'OR'弄乱了,但似乎没有什么是正确的.我试过了

if (num == (1 || 2 || 3))
Run Code Online (Sandbox Code Playgroud)

但它什么都没做.请帮忙!提前致谢.

PS我需要区分几个组.例如...

if (num = (1,2,3))

else if (num = (4,5,6))

else if (num = (7,8,9))
Run Code Online (Sandbox Code Playgroud)

c++ variables comparison if-statement

12
推荐指数
6
解决办法
6万
查看次数

与编译时未解析的字符串文字进行比较

我最近发现了类似于以下几行的内容:

#include <string>

// test if the extension is either .bar or .foo
bool test_extension(const std::string& ext) {
    return ext == ".bar" || ".foo";
    // it obviously should be
    // return ext == ".bar" || ext == ".foo";
}
Run Code Online (Sandbox Code Playgroud)

该功能显然没有按照评论的建议去做。但这不是重点。请注意,这不是Can you use 2 or more OR conditions in an if statement? 因为我完全知道您将如何正确编写函数!


我开始想知道编译器会如何处理这个片段。我的第一个直觉是这将被编译为return true;基本上。将示例插入到Godbolt 中,表明 GCC 9.2 和 clang 9 都没有通过优化进行这种优化-O2。

但是,将代码更改为1

#include <string>

using namespace std::string_literals;

bool test_extension(const std::string& ext) { …
Run Code Online (Sandbox Code Playgroud)

c++ string compiler-optimization c++14

8
推荐指数
1
解决办法
205
查看次数

C++ if语句中的多个条件

我对C++编程的概念很陌生.我希望有一个多条件if语句使用|| (或)和&&(和)在一个陈述中.当我问我的大学教授时.她说这是可能的,然后侮辱了我对这个问题的有限知识.我有权访问的所有示例都显示了一个multi &&语句,只有一个显示了||.它没有显示它们一起使用.我想学习如何使线路正常工作.我会附上我的代码.问题区域是编码的最后一点.

# include <iostream>
# include <cstring>

using namespace std;

main()
{

    const int maximumHours = 774;
    char customerPackage;
    double hoursUsed = 0,
           packageA = 9.95,
           packageB = 14.95,
           packageC = 19.95,
           overPackageA = 2.00,
           overPackageB = 1.00,
           overTime = 0,
           amountDue = 0,
           excessCharged = 0;

    cout << "Please enter the customer's package: ";
    cin >> customerPackage;

    switch (customerPackage)
    {
        case 'a' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break; …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement

6
推荐指数
3
解决办法
7万
查看次数

为什么将三个变量与==进行比较会得出false?

以下程序的输出是“不相等”,但是我希望“他们是平等的”三个变量相比(x,y,和z)相等。为什么?

#include <iostream>

int main()
{
    int y, x, z;
    y = 3;
    x = 3;
    z = 3;

    if (x == y == z)
    {
        std::cout << "they are equal\n";
    }
    else
    {
        std::cout << "they are not equal\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
96
查看次数

具有逻辑OR的IF语句

if(1 == 2 || 4)
{
cout<<"True";
}
else
{
cout<<"False";
}
Run Code Online (Sandbox Code Playgroud)

这就是我如何阅读上述内容.如果1等于2或4,则打印为true.否则,打印错误.执行此操作时...打印为true.显然我在这里误解了一些东西.1不等于2或4.这不是假的吗?

c++ if-statement logical-operators

0
推荐指数
1
解决办法
3万
查看次数

为什么简单的代码不能直接用 C++ 运行?

#include <iostream>
bool check_number(unsigned short int a, unsigned short int b) {
    if ((a || b == 30) || a + b == 30) {
        return true;
    }
    return false;
}

int main() {
    
    std::cout << check_number(15, 16) << std::endl;
    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么我得到“真实”结果?为什么不假呢,我们知道15+16就是31

c++ boolean

0
推荐指数
1
解决办法
86
查看次数