gpt4 book ai didi

c++ - 为设置比较器获取 "Debug Assertion Failed!"

转载 作者:可可西里 更新时间:2023-11-01 18:04:41 26 4
gpt4 key购买 nike

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

我正在使用 VS2010,我的发布二进制文件运行良好,没有任何问题,但调试二进制报告:

enter image description here

我的比较器是这样的:

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() 函数来比较存储在我的集合中的宽字符串吗?

提前致谢!!!

最佳答案

std::set需要一个行为类似于 operator< 的有效比较器或 std::less .

std::set 代码检测到您的运算符<无效,并且作为对您的帮助触发了您显示的断言。

确实:你的计算器看起来像一个 operator!= , 不像 operator<

规则之一operator<应该遵循的是a<bb<a不可能都是真的。在您的实现中,它是。

将您的代码更正为:

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

你应该没问题。

关于c++ - 为设置比较器获取 "Debug Assertion Failed!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5823469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com