根据定义,只有当向量按根据二分搜索的谓词排序时,才能进行二分搜索。
所以,除非你的向量中的所有元素“S.attribute1 > someVariable”都位于所有不是的元素之后,否则这将是一个非首发,就在门。
如果向量中的所有元素都以其他方式排序,那么“其他方式”是唯一可以实现的二进制搜索。
假设它们是,您必须使用某种比较器,该比较器在属性上指定严格的弱排序,以便首先提出您的排序向量:
class comparator {
public:
bool operator()(const your_class &a, const your_class &b) const
{
return a.attribute1 < b.attribute1;
}
};
诀窍是,如果你想单独使用属性值进行搜索,你需要使用一个可与std::binary_search 一起使用的比较器,即defined as follows:
template< class ForwardIt, class T, class Compare >
bool binary_search( ForwardIt first, ForwardIt last,
const T& value, Compare comp );
要使 std::binary_search 成功,范围 [first, last) 必须是
至少部分有序,即它必须满足以下所有条件
要求:
对于所有元素,如果 element
所以,唯一的要求是 comp(value, element) 和 comp(element, value) 需要工作。您可以传递T 的属性值,而不是向量中的整个元素进行搜索,只要您的比较器可以处理即可:
class search_comparator {
public:
bool operator()(const your_class &a, const attribute_type &b) const
{
return a.attribute1 < b;
}
bool operator()(const attribute_type &a, const your_class &b) const
{
return a < b.attribute1;
}
};
现在,您应该可以使用search_comparator 代替comparator,并按属性值进行二分搜索。
而且,正如我所说,如果向量没有按给定属性排序,那么所有的赌注都会被取消。在这种情况下,您需要首先明确地使用std::sort,或者提出一些自定义容器,以正确的顺序单独跟踪向量元素,并且除了包含它们的主向量之外。也许使用指针,在这种情况下,您应该能够使用类似的搜索比较器对指针本身执行二进制搜索,而不是查看指针。