對不起。這個問題是由我粗心造成的。請忽略它,謝謝。在C++中,條件運算符不支持指針?
我嘗試解決問題後的LintCode,Add Two Numbers。
首先,我試試這些代碼:first_code,但我得到了運行時錯誤,並且錯誤消息TERMSIG = 11.
然後,我將這些代碼更改爲second_code。 令我驚訝的是,它的工作。
唯一的區別是:
[首頁]
l1 = (l1) ? nullptr : l1->next;
l2 = (l2) ? nullptr : l2->next;
[二]
if(l1){
l1 = l1->next;
}
else{
l1=nullptr;
}
if(l2){
l2 = l2->next;
}
else{
l2=nullptr;
}
所以我不知道是否有條件的運營商的支持指針或不是?
我試試這些簡單的代碼:
#include <iostream>
int main(){
int* a = nullptr;
int b = 0;
b = (a) ? 1 : 10;
std::cout << b << std::endl;
}
它的工作。
但是爲什麼它在LintCode上的問題上失敗了。 我忽略了什麼?
在你的第二行'l1 =(l2)? nullptr:l2-> next;'你的意思是分配給'l2'嗎? – CoryKramer
[首]還不等於[第二]。 [首先]應該是'l1 =(l1)? l1-> next:nullptr;'或'l1 =(!l1)? nullptr:l1-> next;' – LogicStuff
查看[Ternary條件運算符](http://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator)。 – LogicStuff