I’m implementing a game. I have a state tree and a set<> based priority queue that sorts the states on their costs. For this I have the < operator implemented as:
struct DereferenceCompareState :
public std::binary_function<State*, State*, bool>
{
bool operator()(const State* lhs, const State* rhs) const
{
verbose6("comparing " << lhs->getCost() << " with " << rhs->getCost());
return lhs->getCost() < rhs->getCost();
}
};
Now, I want to eliminate states that have higher cost than some number, but for this, I have to create a dummy state with the cost I want and do an upper_bound on the set. Is there a way to define an operator that works on state vs integer comparisons by for example adding to the struct above? Thanks
You can easily add more flexible comparison ability to
struct DereferenceCompareStateby overloadingoperator()more, but it won’t help with what you’re trying. The real problem you’ve come across is thatstd::set<T,Cmp>::upper_bound()only takes a key/value type (T) as argument. So no matter what you do to your comparison type, you won’t be able to calls.upper_bound(5);The best thing to do is probably to just use a temporary State object like you described.