在IF语句条件下循环

And*_*doc 1 c++ if-statement qt4

我只是想知道循环是否有一种方法可以在If语句条件下?

样品:

if((string.contains(stringlist.hello().value(0),Qt::CaseInsensitive))||(string.contains(stringlist.hello().value(1),Qt::CaseInsensitive))||(string.contains(stringlist.hello().value(2),Qt::CaseInsensitive)))
{
...
}
Run Code Online (Sandbox Code Playgroud)

成为:

if
(
for(int i=0; i < stringlist.hello().size(); i++)
{
string.contains(stringlist.hello().value(i),Qt::CaseInsensitive)
}
)
{
...
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,hello()函数从数据库中检索数据列表.此程序的目的是检查字符串是否包含数据库中的某些关键字.

Chr*_*ris 8

那段代码不会编译; 相反,您可以尝试使用检查每个条件的解决方案并将结果存储到变量中,以确定是否满足条件:

bool testCond = false;
for(int i=0; i < stringlist.hello().size(); i++)
{
    if (string.contains(stringlist.hello().value(i),Qt::CaseInsensitive))
    {
        testCond = true;
        break;
    }
}
if (testCond)
{
    // code here if any of the conditions in the for loop are true
}
Run Code Online (Sandbox Code Playgroud)

我将我的代码更改为使用bool而不是int,因为它看起来像是在使用C++.