Bat*_*n05 5 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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
B++ == 0
Run Code Online (Sandbox Code Playgroud)
这是一个布尔表达式,导致true或false.在这种情况下,结果是true,true然后添加到A.因此(粗略)等价的值true是1:
if(B == 0)
A += 1;
++B;
Run Code Online (Sandbox Code Playgroud)
请注意,阅读代码并不是特别好或不清楚,写这篇文章的人应该被扔进古拉格斯.