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(?));
}