I expect you can write a comparator like this:
struct LengthComparator { bool operator()(const std::string &lhs, std::string::size_type rhs) { return lhs.size() < rhs; } bool operator()(std::string::size_type lhs, const std::string &rhs) { return lhs < rhs.size(); } bool operator()(const std::string &lhs, const std::string &rhs) { return lhs.size() < rhs.size(); } };
Then use it:
std::equal_range(words.begin(), words.end(), length, LengthComparator());
I expect the third operator() overload to never be used, as the information it provides is redundant. The range must be pre-sorted, so there is no algorithm for comparing two elements from a range, it should be a comparison of elements from a range against the target that you set. But the standard does not guarantee this. [Edit: and the definition of all three means, you can use the same comparator class to first put the vector in order, which can be convenient].
This works for me (gcc 4.3.4), and although I think it will work on your implementation too, I'm not sure if it really is valid. It implements the comparisons that will be described in the equal_range description, and the result of 25.3.3 / 1 does not require that the template parameter T be exactly the same type of objects referenced by iterators. But there may be some text that I skipped that adds more restrictions, so I would do more standard trawling before using it in something important.
source share