2017-01-01 54 views
-1

事情是這樣的:如何讀取/解釋複雜的表達式在C++

int ctr[], i, distinct = 0; 

string s; 

distinct += ctr[s[i]]++ == 0; // <-- This line 

我明白陣列,什麼增​​量和減量運算符的符號。我只是不明白這樣寫的方式。更重要的是,寫作的目的就是這樣。

+5

有人寫這段代碼'distinct + = ctr [s [i]] ++ == 0''-出去喊他 –

+0

由於源代碼級優化,看起來有些代碼被混淆了。我想一個好的編譯器會在AST/Assembly級別上做類似的事情。 –

+1

http://en.cppreference.com/w/cpp/language/operator_precedence –

回答

0

它像下面的代碼:

char c = s[i]; 
distinct = distinct + ctr [x]; // "value +=" is the same as "value = value + ..." 

ctr[c]++; // char is used as number here. The c. element in ctr[] is incremented. 
//because of c++ instead of ++c the increment is done after all. 
distinct == 0 // returns a boolean value into nirvana :) 

我希望這會幫助你。

但是,如果你想編碼,你不應該寫這樣的代碼。特別是如果你想在未來閱讀你的代碼或希望其他人閱讀它;-)

+0

請閱讀有關運算符優先級的內容。首先完成增量。比較是最後的操作。你的回答不正確 –

+0

是的,今晚晚了一點,對不起,你說得對! – Christian

+0

現在它產生了一個不同的錯誤(與其他答案相同的錯誤) - '=='的結果是添加到'distinct'的結果 –