1

I am trying to use lower_bound on a vector of a struct.

struct order_book
{
    string action;
    long order_id;
    string side;
    long quantity;
    long price;
};

vector<order_book> buy_side;
vector<order_book> sell_side;

I am not sure how to construct the comparison function. I want to compare "price". I have seen examples where integers from an array were used. It is very straightforward. However, I am confused here as to how should I set my comp function.

Would this work?

bool compf(order_book &a, order_book &b){
return a.price< b.price;
}

part of code where I have used the lower_bound:

//we pass a "temp" vector to this function.
 auto loc=lower_bound(buy_side.begin(),buy_side.end(),stoi(temp.at(4))); 
//stoi(temp.at(4)) is the price of a new order which we want to compare.

if(buy_side.empty() == 0){
buy_side.push_back(order_book{temp.at(0),stoi(temp.at(1)),temp.at(2),stoi(temp.at(3)),stoi(temp.at(4))});
}
else{
buy_side.insert(loc,order_book{temp.at(0),stoi(temp.at(1)),temp.at(2),stoi(temp.at(3)),stoi(temp.at(4))}//, compf(?));
}
  • From [this `std::lower_bound` reference](https://en.cppreference.com/w/cpp/algorithm/lower_bound) it can be read that there's an overload taking a fourth argument, being a pointer to the compare function. And that the second argument to the compare function is the *value* to compare with (the one passed to the `std::lower_bound` function as the third argument). – Some programmer dude Sep 06 '21 at 04:18
  • 1
    To use `lower_bound` this way, you need something like `bool price_comp(const order_book& order, long price) { return order.price < price; }`, and then you do `std::lower_bound(buy_side.begin(),buy_side.end(), price_to_find, price_comp)`. Note that the vector must be sorted by `price`. – Igor Tandetnik Sep 06 '21 at 04:19
  • 1)Why is it that the vector must be sorted by price? 2) I am trying to populate the vector in a sorted manner. When the new element is populated in vector, we will use the lower_bound method to find the location, and then we will insert it. Thanks for your comment @IgorTandetnik So when new element is added, the vector will be already sorted. – Ninad Pethkar Sep 06 '21 at 04:23
  • @NateEldredge: Ah. sorry. corrected the mistake. thanks. – Ninad Pethkar Sep 06 '21 at 04:23
  • Just a hint, if you've got a very large number of books then using std::list may be more efficient for inserting the books. (https://stackoverflow.com/questions/2209224/vector-vs-list-in-stl) – Pepijn Kramer Sep 06 '21 at 04:32
  • How can we check the value of the element that the iterator points to? Here I would like to check if the price of the element to which iterator returned from the lower_bound method points is equal to the price that we are searching. If the price is same then we will insert that new element behind current one. – Ninad Pethkar Sep 06 '21 at 04:54
  • Have you ever looped over a container (like a vector) using iterator? You access the object "pointed to" by the iterator returned by `std::lower_bound` like that loop-iterator: By dereferencing it, or using the `->` access operator. And both vectors and lists have an "insert after this iterator" function. – Some programmer dude Sep 06 '21 at 05:18

0 Answers0