I have a sorted collection of
class Thing
{
public:
item a;
item b;
other data;
};
vector<Thing> Things;
using
class MultiValuedComparator
{
public:
item a;
item b;
MultiValuedComparator(item c, item d)
{
a = c;
b = d;
}
};
Since I have duplicates of item a, and item b (but not other data), I want to grab a range of those data structures that match item a AND item b. The collection is sorted by item a only.
I thought equal_range would be an appropriate method to do this. Since I needed to match more than one item I inherited from binary_function.
struct RangeByA : public std::binary_function<Thing, MultiValuedComparator>
{
bool operator()(const Thing &left, const MultiValuedComparator &right)
{
return left.a == right.a && left.b == right.b;
}
}
I don't know how to write the equal_range function so it does this. I tried:
void somefunction()
{
typedef pair<vector<Thing>::iterator,
vector<Thing>::iterator> startEndIterPair;
MultiValuedComparator mvc(1, 2);
startEndIterPair p = equal_range
(
Things.start(),
Things.end(),
std::bind2nd(RangeByA, mvc)
);
}
but this code complains of no match for 'operator<' in '_middle._gnu_cxx::__normal_iterator .. etc at the call to equal_range
How do I write this so equal_range will work? I have no idea where to place the overloaded operator. RangeByA does not seem to accept it.