【发布时间】:2012-04-22 21:46:10
【问题描述】:
我有一个相对简单的算法,它遍历 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;
}
}
此时:
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;
}
}
没有。这是为什么?我希望像这样的两个 NULL ptr 在被否定时一起评估为真。
【问题讨论】: