更改 std::set 以按 T 类型查找项目
Change std::set to find item by T type
template<class T>
class State {
T state;
double cost = 0;
State<T> *cameFrom = nullptr;
我有这个模板类,我想创建一个std::set<State<T>>
类的<
运算符返回this.cost < other.cost
类的==
运算符返回this.state == other.state
假设我想检查State<T> x
是否在一个集合中
我怎样才能使集合返回一个 iter !- end()(调用 set.find(x)
) 如果集合包含与 x
具有相同state(x.state)
的State<T>
?
std::set
不关心operator==
,它使用等价关系定义为具有与
bool equiv(T a, T b)
{
if (a < b) return false;
if (b < a) return false;
return true;
}
如果您有State<T>
值,state
不同但cost
相等,则只有一个可以进入您的std::set
。
您可能会在boost::multi_index_container
上取得成功,您可以在其中按cost
或state
查找内容
namespace bmi = boost::multi_index;
using state_set = boost::multi_index_container<State<T>,
bmi::indexed_by<
bmi::ordered_unique<bmi::member<State<T>, double, &State<T>::cost>>,
bmi::ordered_non_unique<bmi::member<State<T>, T, &State<T>::state>>
>
>;
state_set set = ...;
auto & states_view = set.nth_index<1>();
if (auto it = states_view.find(x); it != states_view.end())
...
相关文章:
- 模板元程序查找相似的连续类型名称
- 查找声明为结构类型 C++ 的数组末尾
- 按类型与字符串查找对象
- 了解元函数以在类型包中查找类型
- 如何在C++中查找类型的定义
- MFC,查找特定文件类型
- 从模板实例化/类型推断中查找错误消息的实际来源
- 查找上下文RPC_NS_HANDLE未知类型名称
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 在数据类型类的列表中查找 elem
- 查找类型,is_constructible持有的类型
- 如何使用 std::find 从向量的第一个元素中查找字符串<字符串对<字符串、字符串> >类型?
- 使用查找表选择具有运行时索引的可变参数类型
- 如何查找一组类型对是否包含一个元素作为第二个成员
- 通过与不同类型的值进行自定义比较来查找 std::set 的元素
- 更改 std::set 以按 T 类型查找项目
- unique_ptr<int[]>、vector <int>和 int[] 的模板化类型查找
- 模板和嵌套类型查找
- 使用接口类型查找子窗口小部件
- 如何在c++中使用映射实现模板的返回类型查找