设置类忽略定义的 comp fn 和默认比较中的错误
set class ignoring defined comp fn and errors on default comparison
>我正在使用与 STL Set 类基本相同的自定义 Set 类
问题是我不知何故不正确地实现了它,并且使用了默认的比较函数而不是我定义的比较。
Set<Lexicon::CorrectionT> Lexicon::suggestCorrections(Lexicon::MatchesT & matchSet)
{
Set<CorrectionT> suggest(compareCorr); //ordered Set
suggestCorrectionsHelper(root, suggest, 0, matchSet.testWord, 0, matchSet.testTime);
return suggest;
}
int compareCorr(Lexicon::CorrectionT a, Lexicon::CorrectionT b)
{
if (a.editDistance < b.editDistance)
return -1;
else if (a.editDistance == b.editDistance)
return 0;
else
return 1;
}
struct CorrectionT {
int editDistance; //stackItems
string suggestedWord; //stackItems
};
一些研究:
- 类库
- 同一类的问题 - 建议使用 STL Set 类,在这种情况下,我可能仍然需要帮助了解如何定义比较函数,因此发布问题
都与某种形式的"错误 C2784:'bool std::operator ==(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &(' 有关:无法从"词典::更正T"中推断出"const std::_Tree<_Traits> &"的模板参数
并引用此代码
template <typename Type>
int OperatorCmp(Type one, Type two) {
if (one == two) return 0;
if (one < two) return -1;
return 1;
}
我的问题:你建议如何纠正这个问题?
尝试更改默认定义类型:
#include "lexicon.h"
//template <typename Type>
int OperatorCmp(Lexicon::CorrectionT one, Lexicon::CorrectionT two) {
if (one.editDistance == two.editDistance) return 0;
if (one.editDistance < two.editDistance) return -1;
return 1;
}
#endif
STL set
的比较函数必须是严格弱排序,因此该类与 STL set
不同。
该错误指示正在使用默认OperatorCmp
,我不知道为什么,但是您可以通过为CorrectionT
类型定义operator<
和operator==
来使默认起作用。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- 没有默认构造函数作为模板参数的自定义比较器
- 用户定义的结构是否有默认C++比较器?
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 在priority_queue中使用默认容器但自定义比较器
- 如何比较C 中的两个字符串并使用默认功能
- 为什么<wstring>使用自定义 wcscmp 和 wmemcmp 比较器对向量进行排序比默认快得多?
- 为什么要使用std ::少用作默认函数来比较std :: map and std :: set中的键
- 在Template中设置默认比较类型
- 如何在自己的类中使用默认比较器
- 设置类忽略定义的 comp fn 和默认比较中的错误
- 为什么 std::sort 有两个实现(带和不带比较器),而不是一个带有默认模板参数的实现
- 当我只对比较部分值感兴趣而不能假设另一部分的默认构造函数时,如何使用 std::lower_bound
- C++/STL默认比较器
- 不能与默认构造的右值进行比较
- 使用非默认比较谓词的集合容器
- 如何在STL中设置自定义的默认比较函数