gpt4 book ai didi

c++ - std::sort 函数给出 "Bus error: 10"

转载 作者:太空狗 更新时间:2023-10-29 20:56:13 24 4
gpt4 key购买 nike

以下代码给出了“总线错误:10”,尽管当 n 更改为 30 时它工作得很好。我真的没有看到该错误的任何单一原因。您认为为什么会发生这种情况?

#include <algorithm>
#include <cstdio>
#define MAX 100000
using namespace std;

struct suffix
{
int cur;
};

suffix suffixes[MAX];

bool cmp(suffix a, suffix b)
{
return (a.cur <= b.cur);
}

int main()
{
int n = 1000;
sort(suffixes,suffixes + n,cmp);
return 0;
}

最佳答案

您的比较函数有问题。它不满足 the requirements std::sort 预期。它需要给出一个严格的弱排序,它必须为等效元素返回false。尝试将其更改为:

bool cmp(suffix a, suffix b)
{
return (a.cur < b.cur); // note comparison is < instead of <=
}

关于c++ - std::sort 函数给出 "Bus error: 10",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102018/

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