c++ - 了解简写 C++

标签 c++ shorthand

<分区>

我的 C++ 书中有这段代码,但我不确定这段代码的含义:

for ( ; counter < fooCnt &&
        (toLower(array[counter].getFooTitle()).find(x) == string::npos)
      ; counter++);

这一切都在一行中,是否可以用另一种方式编写这段代码?我也不明白为什么会有一个“;”在 for 循环开头的变量之前....

最佳答案

clause 1在 for 循环中是可选的。它说循环直到array[counter].getFooTitle()).find(x)不等于 string::nposcounter >= fooCnt

&&是短路 AND 运算符。如果您忘记了这部分,请返回您的真值表。

counter < fooCnt && (toLower(array[counter].getFooTitle()).find(x)==string::npos)是表达式 2 和 counter++是表达式-3

counter从而递增。

6.8.5.3 C标准的:

1774 The statement

    for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows:

1775 The expression >expression-2 is the controlling expression that is evaluated before each execution of the loop body.

1776 The expression expression-3 is evaluated as a void expression after each execution of the loop body.

1777 If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions;

1778 it is reached in the order of execution before the first evaluation of the controlling expression.

1779 If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.134)

1780 Both clause-1 and expression-3 can be omitted.

1781 An omitted expression-2 is replaced by a nonzero constant.

顺便说一句,for 循环也可以被认为是 while 循环。

关于c++ - 了解简写 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286187/

相关文章:

c++ - 简单的 'keypress' 程序

c++ - std::allocator_traits::construct 调用了错误的构造函数

javascript - 为像 php 这样的键填充一个 javascript 数组速记

c++ - 缩短 C++ 枚举成员的路径(使用 typedef 或 typename),用作模板参数

wpf - 将 ValidationRules 添加到单个 xaml 行或简写 ValidationRules

有人可以解释在 Pebble C Watchface 教程中找到的部分代码吗?

c++ - QMediaplayer 从自定义 QIODevice 流式传输,在 Mac OS (10.9) 上加密

c++ - GetAdaptersAddresses() 未给出正确的 IP 地址

C++, union 与类继承

mysql - 简化/速记 SQL 数据定义语言?