在运行时为 std::set 定义自定义比较器
Define custom comparator at run time for std::set
我需要帮助才能在运行时定义自定义标准::设置比较器。我知道如何定义具有固定值的基本比较器。我真的坚持如何在运行时做到这一点。我正在运行Visual Studio 2010,如果有帮助的话。
下面是我的代码:
#include <set>
struct CustomComp
{
CustomComp():m_tolerance(0.1){}
//Always assume tolerance >= 0.0
CustomComp(double const &tolerance):m_tolerance(tolerance){}
/*Only return true when the two number are sufficiently apart from each other*/
bool operator()(double const &n1, double const &n2)
{
double diff = n1 - n2;
if(diff < 0.0 && std::abs(diff) > m_tolerance) return true;
if(diff > 0.0 && std::abs(diff) > m_tolerance) return false;
return false;
}
private:
double m_tolerance;
};
int main(int argc, char **argv)
{
/*This works */
std::set<double, CustomComp> aaa;
aaa.insert(0.0);
aaa.insert(0.2);
aaa.insert(0.3);
aaa.insert(10.0);
/*What I really want*/
double tol = GetToleranceFromUser();
std::set<double, CustomComp(tol)> bbb;
return 0;
}
谢谢。
比较器作为参数传递给集合构造函数:
std::set<double, CustomComp> bbb(CustomComp(tol));
相关文章:
- Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让原生C / C++
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- C++定义自定义unordered_set的不同方法
- 如何为 std::unordered_set 定义自定义键等效谓词?
- 如何从设备代理定义自定义片段
- 如何定义自定义跨平台size_t类型
- C++ - 定义自定义新建和删除运算符时make_shared
- C++ - 定义自定义析构函数时程序崩溃
- LLDB调试器 - 定义自定义类型显示
- 如何在 Boost Dijkstra 中定义自定义距离
- 为boost哈希映射定义自定义哈希函数
- 在派生类中定义自定义返回类型的受保护方法
- 我可以为我的自定义Qt UI元素定义自定义CSS/QSS属性吗?
- 如何在C++中定义自定义浮点格式(类型)
- 定义自定义扫描运算符
- 在运行时为 std::set 定义自定义比较器
- 如何在 C(++) 中定义自定义错误代码
- 如何为Apache模块定义自定义配置指令
- 声明/定义自定义类定制对象的正确方法
- 为boost中的动态阵列定义自定义步进器