[英]What does this arithmetic expression mean: A += B++ == 0 in C++;
我來到這個表達式,並且無法理解以下代碼段中第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
這是一個布爾表達式,導致true
或false
。 在這種情況下,結果為true
,然后將true
添加到A
true
值為1
因此(粗略)等效值為:
if(B == 0)
A += 1;
++B;
請注意,閱讀代碼並不是特別好或不清楚,寫這篇文章的人應該被扔進古拉格斯。
讓我們把這個表達式分成幾部分: A += value
,而value = B++ == 0
。 正如后來的cout
建議的那樣, value == 1
。 這是為什么? 原因如下: value
是B++
和0
的比較結果,但++
(增量)操作,當在操作數之后寫入時,正在進行比較后處理,即如果你寫A += ++B == 0
則后來的cout
應該(並且確實)打印0, 1
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.