通过与不同类型的值进行自定义比较来查找 std::set 的元素
Find elements of std::set by custom comparison with value of different type
考虑以下带有自定义比较器的std::set
玩具示例:
#include <set>
struct A {
A() : a(cnt++) {}
const int a;
static int cnt;
};
int A::cnt = 0;
struct comp {
bool operator()(const A& left, const A& right)
{
return left.a < right.a;
}
};
int main()
{
std::set<A, comp> sa;
for (int i = 0; i < 10; ++i) sa.insert(A());
return 0;
}
请注意,不能简单地从整数创建A
。
我想在 sa
中寻找给定值为 A::a
的A
,而不构造类型 A
的临时对象,即我正在寻找类似
sa.find(4)
使用自定义比较器,允许将整数与类型A
的对象直接进行比较。这可能吗?
使用 C++14,您可以使用"透明"比较器:
#include <iostream>
#include <set>
#include <type_traits>
class A
{
public: explicit A() : a{cnt++} {}
private: explicit A(int) = delete;
public: const int a;
private: static int cnt;
};
int A::cnt{};
class Comparator
{
// this member is required to let container be aware that
// comparator is capable of dealing with types other than key
public: using is_transparent = std::true_type;
public: bool operator()(const int & left, const A& right) const
{
return left < right.a;
}
public: bool operator()(const A & left, const int& right) const
{
return left.a < right;
}
public: bool operator()(const A& left, const A& right) const
{
return left.a < right.a;
}
};
int main()
{
std::set<A, Comparator> sa{};
for (int i{}; i < 10; ++i)
{
sa.emplace();
}
std::cout << sa.find(3)->a << std::endl;
return 0;
}
在线编译器
在 C++14 之前,异构查找在::boost::intrusive::set
中可用:
#include <boost/intrusive/set.hpp>
#include <iostream>
namespace bi = ::boost::intrusive;
// hook contains set node data, supports various options, can be a member
class A: public bi::set_base_hook
<
bi::link_mode<bi::link_mode_type::safe_link>
>
{
public: explicit A() : a{cnt++} {}
private: explicit A(int) = delete;
public: const int a;
private: static int cnt;
};
int A::cnt{};
class Comparator
{
public: bool operator()(const int & left, const A& right) const
{
return left < right.a;
}
public: bool operator()(const A & left, const int& right) const
{
return left.a < right;
}
public: bool operator()(const A& left, const A& right) const
{
return left.a < right.a;
}
};
int main()
{
bi::set<A, bi::compare<Comparator>> sa{Comparator{}};
for (int i{0}; i < 10; ++i)
{
sa.insert(*new A{}); // typically user manages object creation
}
// comparators may vary
std::cout << sa.find(3, Comparator{})->a << std::endl;
return 0;
}
在线编译器
相关文章:
- C++ 将二进制字符串转换为整数或比较 2 个字符串以查找差异数
- 查找和比较两个字符串中的字符
- 在没有比较运算符的情况下查找 2 个数字之间的最小值
- 在查找子集中元素和元素数量之间的二进制比较背后的逻辑是什么?
- 通过与不同类型的值进行自定义比较来查找 std::set 的元素
- 如何实现 std::map::查找包含两个结构'Pos'结构的比较逻辑,每个结构包含 x 和 y 坐标
- 我们可以使用异构查找比较器对 STL 关联容器执行"partial-match"搜索吗?
- STD :: SET(或STD :: MAP)是否有一个提供基于值的查找的STL比较器?STD :: lands_les
- 在std ::查找方法中的类对象的比较
- 映射与对象键,查找和比较功能
- C++ 标准::查找自定义比较器不起作用
- std::map:仅使用部分键类型进行比较和查找
- O(N) 查找,但在排序列表上使用 upper_bound 时进行 O(log(N)) 比较
- 是否有映射的查找()以使用带参数的比较器
- 比较字符串中的单个字符.在用户输入的字母字符串中查找辅音
- 比较数字并查找具有相同数字的数字
- 在std::set中查找由自定义比较器设置的元素
- 更好的std::在指针集合上查找,并将取消引用的值与常量引用值进行比较
- 通过使用 MPI 环形拓扑比较每个处理器的局部最大值来查找函数的全局最大值
- 在 stl 映射中查找在编写自定义比较函数时不起作用,该函数会阻止键排序