对结构向量的二进制搜索
Binary search on the vector of structs
我有一个结构向量,其中包含具有此架构的结构
struct Main{
int mainID;
string mainDIV;
string mainNAME;
}
可以对struct使用二进制搜索吗?我知道使用很容易实现价值
binary_search( vector.begin() , vector.end() , 5 )
但有没有一种方法可以通过回调或其他方法来实际找到结构的属性?我找不到任何与这个话题有关的东西。
是的,这是可能的。std::binary_search
采用的value
只有在与容器的元素相比较时才有意义。在简单的情况下(如果Main
在某个地方支持operator<
),您将提供Main
类型的元素作为值:
// check if this specific Main exists
bool yes = std::binary_search(v.begin(), v.end(), Main{0, "some", "strings"});
// does exactly the same thing as above
bool yes = std::binary_search(v.begin(), v.end(), Main{0, "some", "strings"}
, std::less<Main>{});
如果它不支持operator<
(或者你的容器是由其他东西订购的,例如mainID
),那么你必须自己提供一个算法将使用的比较器:
// check if there is a Main with mainID 5
bool yes = std::binary_search(v.begin(), v.end(), 5,
[](const Main& element, const int value) {
return element.mainID < value;
});
您必须向binary_search()
提供信息,告诉它如何比较您的对象。两种最常见的方法是,如果可能的话,在struct
中添加一个operator<()
,或者提供一个可以比较两个struct
的辅助功能。
第一种形式看起来像这样:
struct Main {
int mainID ;
string mainDIV ;
string mainNAME ;
bool operator<(const Main & other) const
{
return mainID < other.mainID ;
}
}
这只会在mainID
上进行比较,但您可以从那里展开它。
此外,这只教编译器如何比较两个struct Main
,而@Barry上面的答案将匹配int和struct Main
。但让我们继续这个答案。
现在要找到5
的记录,我们必须将其制作成struct Main
:
struct Main search_key = { 5 } ;
bool yes = std::binary_search( v.begin(), v.end(), search_key ) ;
现在,这不是很优雅,而且如果你有struct Main
的构造函数(并且还没有把它放在你的例子中),这甚至都不起作用。因此,我们只为int
添加了另一个构造函数。
struct Main
{
Main(int id, const string & a_div, const string & a_name ) : id(id), div(a_div), name(a_name) { }
Main(int id) : id(id) { }
int id ;
string div, name ;
bool operator<(const Main &o) const { return id < o.id ; }
} ;
现在我们可以做一个稍短的形式:
bool has_3 = std::binary_search( v.begin(), v.end(), Main( 3) ) ;
历史笔记:Bjarne已经尝试将默认比较运算符纳入标准相当长一段时间了,但在标准会议上并不是每个人都对此感到兴奋。我认为在上次会议上在这方面取得了一些进展,所以当C++17成为一种东西时,它可能最终会出现。
- 有根的二进制搜索树.保留与其父级的链接
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- 我的二进制搜索程序只是关闭了
- 二进制搜索的终止点
- 递归二进制搜索与字符串数组
- 测试树是否为二进制搜索树
- 如何二进制搜索结构向量并在适当的索引处插入
- 如果你有一个固定大小的数组,你需要遍历它!n次,使用二进制搜索如何改变时间复杂性
- 字符串的递归二进制搜索-C++
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 二进制搜索树没有匹配的函数调用
- 二进制搜索不适用于特定输出
- 如何在向量上进行二进制搜索以找到具有特定id的元素
- 二进制搜索树不变的标头文件
- 二进制搜索树插入带参数参考
- 二进制搜索不变以查找元素的首次出现
- 了解一种神秘地起作用的递归二进制搜索算法