我遇到了这个表达式,无法理解以下代码段中第 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 表达式,结果为 true
或 false
。在这种情况下,结果为 true
,然后将 true
添加到 A
。 true
的值为 1
因此(粗略)等价物为:
if(B == 0)
A += 1;
++B;
请注意,这不是特别好或清晰阅读代码,编写此代码的人应该被扔进古拉格。
关于c++ - 这个算术表达式是什么意思 : A += B++ == 0 in C++;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175219/