我想做以下事情:
time_heap.insert(aid.arrival(event)!=NULL);
aid.arrival(event)
即,如果不是,则将的返回值插入到 time_heap 中NULL
。
这是一个将在我的程序的主控件中经常发生的操作,并且希望在 C++ 中有一种简便的方法来完成它(除了定义我自己的函数来处理它)
186 void insert_event(Event* value) {
187 heap.push_back(value); // expand size of heap
188 int i = heap.size() - 1; // set heap index to that of "value"
189 int parent = floor((i - 1)/2);
190
191 while (parent >= 0 && parent < heap.size()) { //check that parent is valid
192 if (*heap[parent] > *value) {
193 heap[i] = heap[parent];
194 heap[parent] = value; // if "value" is smaller than parent move it up in heap (swap)
195 i = parent; // set new index of "value"
196 parent = floor((i - 1)/2); // set new parent of "value"
197 }
198 else // if parent is not larger, value satisfies min-heap condition (since all below are lower, too)
199 break; // (i.e. we are done)
200 }
201 }