【发布时间】:2018-10-14 23:00:25
【问题描述】:
遇到了这个表达式,看不懂下面sn-p中第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;
}
【问题讨论】:
-
意味着您的 CS 老师以错误的方式教您错误的东西。用这些C++ books 补充您的正规教育。
-
这意味着将最后一个值 -
B++结果 - 与 0 进行比较,并返回一个布尔值 true / false。但这是相当愚蠢的代码。 -
这段代码如果你是在工作场合写的,你应该立即被解雇。
-
<< <<看起来不像是有效的 C++。你的意思是<< " " <<? -
@Lundin bool 将被提升为 int;以同样的方式 int 将被提升为 long 等。
标签: c++ arithmetic-expressions post-increment