Jul*_*lik 0 c++ null pointers boolean
我有一个相对简单的算法,它走std :: vector寻找两个相邻的元组.一旦找到X值左右两侧的元组,我就可以在它们之间进行插值.不知何故,这有效:
std::vector<LutTuple*>::iterator tuple_it;
LutTuple* left = NULL;
LutTuple* right = NULL;
bool found = 0;
// Only iterate as long as the points are not found
for(tuple_it = lut.begin(); (tuple_it != lut.end() && !found); tuple_it++) {
// If the tuple is less than r2 we found the first element
if((*tuple_it)->r < r) {
left = *tuple_it;
}
if ((*tuple_it)->r > r) {
right = *tuple_it;
}
if(left && right) {
found = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
而这个:
std::vector<LutTuple*>::iterator tuple_it;
LutTuple* left = NULL;
LutTuple* right = NULL;
// Only iterate as long as the points are not found
for(tuple_it = lut.begin(); tuple_it != lut.end() && !left && !right; tuple_it++) {
// If the tuple is less than r2 we found the first element
if((*tuple_it)->r < r) {
left = *tuple_it;
}
if ((*tuple_it)->r > r) {
right = *tuple_it;
}
}
Run Code Online (Sandbox Code Playgroud)
才不是.这是为什么?我希望像这样的两个NULL pt在被否定时一起评估为true.
一找到第二个循环就会终止.将条件更改为:
tuple_it != lut.end() && !(left && right)
Run Code Online (Sandbox Code Playgroud)
要么
tuple_it != lut.end() && (!left || !right)
Run Code Online (Sandbox Code Playgroud)
继续,直到找到它们.