Code:
vector<weight *> &res;
vector<weight>::iterator it = lower_bound(w.begin(), w.end(), queryweight);
while(it != w.end()) {
weight *w = &(*it);
if(w->weight >= 60) break;
res.push_back(w);
it++;
}
I think the lower_bound do a binary search (?), so in the end, does the C++ code intend to get the weights wanted? Where it starts and stops? And what does the while loop in this case do? thanks!
lower_boundreturns the lowest iterator (i.e. position in the vector) of an element that is not less than the third parameter – here,queryweight. Thewhileloop then goes through the remaining elements and, until it reaches an element that has awightof greater than or equal to 60 adds them to the vectorres. I assume the input vectorwis sorted, otherwise this function wouldn’t make much sense.Line by line: