对不起。这个问题是由我粗心造成的。请忽略它,谢谢。在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