c++ - 这个算术表达式是什么意思 : A += B++ == 0 in C++;

标签 c++ arithmetic-expressions post-increment

我遇到了这个表达式,无法理解以下代码段中第 3 行的含义:

int A=0, B=0;
std::cout << A << B << "\n"; // Prints 0, 0
A += B++ == 0; // how does this exp work exactly? 
std::cout << A << B << "\n";  // Prints 1, 1

A加B,B是Post加1,“==0”是什么意思?

编辑: 这是实际的代码:

int lengthOfLongestSubstringKDistinct(string s, int k) {
    int ctr[256] = {}, j = -1, distinct = 0, maxlen = 0;
    for (int i=0; i<s.size(); ++i) {
        distinct += ctr[s[i]]++ == 0; // 
        while (distinct > k)
            distinct -= --ctr[s[++j]] == 0;
        maxlen = max(maxlen, i - j);
    }
    return maxlen;
}

最佳答案

B++ == 0 

这是一个 bool 表达式,结果为 truefalse。在这种情况下,结果为 true,然后将 true 添加到 Atrue 的值为 1 因此(粗略)等价物为:

if(B == 0)
  A += 1;
++B;

请注意,这不是特别好或清晰阅读代码,编写此代码的人应该被扔进古拉格。

关于c++ - 这个算术表达式是什么意思 : A += B++ == 0 in C++;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175219/

相关文章:

C 编程增量和减量

c++ - 根据镜像效果、亮度和缩放级别读取 jpeg 文件

c++ - 学习 C++ : why is this illegal?

java - C 和 Java 中的后缀和前缀运算符产生不同的结果

arrays - Bash - 数组索引中的算术

C:以下代码的输出是什么?请解释一下

c++ - 从 boost python 模块内的 pyside 导入类?

c++ - AES 库或 C++ 代码

android - 带有括号的算术表达式的计算结果为零

c++ - C++ 的意外输出