获取“调试失败!用于置位比较器 [英] Getting "Debug Assertion Failed!" for set comparator

查看:163
本文介绍了获取“调试失败!用于置位比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似问题已在此链接中得到解答帮助我修复此C ++ std :: set comparator
,但不幸的是,我面临的是完全相同的问题,我无法理解其背后的原因,因此需要一些帮助来解决它。

I know similar issue has been answered at this link Help me fix this C++ std::set comparator but unfortunately I am facing exactly same issue and I am unable to understand the reason behind it thus need some help to resolve it.

我使用VS2010和我的发行二进制运行正常没有任何问题,但调试二进制报告:

I am using VS2010 and my release binary is running fine without any issue but the debug binary reports:

我的比较器如下所示:

struct PathComp {
    bool operator() (const wchar_t* path1, const wchar_t* path2) const
    {
        int c = wcscmp(path1, path2);
        if (c < 0 || c > 0) {
            return true;
        }
        return false;
    }
};

我的组合声明如下:

set<wchar_t*,PathComp> pathSet;

有人可能建议我为什么我的调试二进制文件在这个断言失败?是因为我使用wcscmp()函数来比较存储在我的集合中的宽字符串?

Could somebody suggest me why my debug binary is failing at this assertion? Is it because I am using wcscmp() function to compare the wide character string getting stored in my set?

感谢提前!!!

推荐答案

std :: set 需要一个行为类似于 std :: less

std::set requires a valid comperator that behaves like operator< or std::less.

std :: set代码检测到运算符&

The std::set code detected that your operator< is not valid, and as a help to you triggered the assert you showed.

确实:你的comperator看起来像一个运算符!=

And indeed: your comperator looks like an operator!=, not like an operator<

其中一个规则 a b 不能同时是真正。

One of the rules an operator< should follow, is that a<b and b<a cannot be both true. In your implementation, it is.

请将您的代码更正为:

bool operator() (const wchar_t* path1, const wchar_t* path2) const
{  
  int c = wcscmp(path1, path2);
  return (c < 0);
}

你应该很好。

这篇关于获取“调试失败!用于置位比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆