c++ - 使用 std::sort 时的核心转储

标签 c++

我最近写了c++,解决了在线判断的问题。 排序时它核心转储。看起来很奇怪。不管代码什么时候会被在线法官接受,我很困惑为什么会转储核心,我已经使用了很多次 std::sort 。

下面是代码。

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

struct Pair {
    Pair(int xx, int yy) : x(xx), y(yy) {}

    int x;
    int y;
};

int get_min(int a, int b) {
    return (a <= b) ? a : b;
}

bool pool_compare(const Pair& a, const Pair& b) {
    return a.x + a.y <= b.x + b.y;
}

void get_boundary(const vector<int>& nums1, const vector<int>& nums2, 
                  const int k, int& p, int& q) {
    q = 0;
    p = 0;
    int tail = get_min(k, nums1.size() + nums2.size());
    int cnt = 1;

    while (cnt < tail) {
        if (q + 1 >= nums2.size() || 
            nums1[p + 1] + nums2[q] <= nums1[p] + nums2[q + 1]) {
            p++;
        }
        else {
            q++;
        }
        if (p * q >= k) {
            break;
        }
        cnt++;
    }
}                 

vector<pair<int, int> > kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
    vector<pair<int, int> > ret;
    if (0 == k) {
        return ret;
    }

    int p = 0;
    int q = 0;
    get_boundary(nums1, nums2, k, p, q);

    vector<Pair> pool;
    for (int i = 0; i <= p; i++) {
        for (int j = 0; j <= q; j++) {
            pool.push_back(Pair(nums1[i], nums2[j]));
        }
    }
    std::sort(pool.begin(), pool.end(), pool_compare);

    k = get_min(pool.size(), k);
    for (int i = 0; i < k; i++) {
        ret.push_back(pair<int, int>(pool[i].x, pool[i].y));
    }

    return ret;
}

int main() {

    int a[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1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1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    vector<int> nums2(b, b + sizeof(b) / sizeof(int));

    vector<pair<int, int> > r = kSmallestPairs(nums1, nums2, 1000);
    for (size_t i = 0; i < r.size(); i++) {
        printf("%d\t%d\n", r[i].first, r[i].second);
    }

    return 0;
}

下面列出了调用堆栈,似乎没有太大帮助。

#0  0x0000000000400900 in pool_compare(Pair const&, Pair const&) ()
#1  0x00000000004025c3 in __gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > > std::__unguarded_partition<__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, Pair, bool (*)(Pair const&, Pair const&)>(__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, __gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, Pair, bool (*)(Pair const&, Pair const&)) ()
#2  0x0000000000401ac2 in void std::__introsort_loop<__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, long, bool (*)(Pair const&, Pair const&)>(__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, __gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, long, bool (*)(Pair const&, Pair const&)) ()
#3  0x00000000004011b3 in void std::sort<__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, bool (*)(Pair const&, Pair const&)>(__gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, __gnu_cxx::__normal_iterator<Pair*, std::vector<Pair, std::allocator<Pair> > >, bool (*)(Pair const&, Pair const&)) ()
#4  0x0000000000400b9e in kSmallestPairs(std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, int) ()
#5  0x0000000000400dbb in main ()

最佳答案

你的比较器违反了契约(Contract)。它应该满足strict weak ordering关系。

这意味着它应该产生 false当传递相同的元素时,并且 cmp(a,b)cmp(b,a)永远不会屈服true两次都是相同的ab

经验法则:不要使用<=对于比较器;使用<相反。

关于c++ - 使用 std::sort 时的核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933036/

相关文章:

类似于 C++ 中的 C# 方法指针

c++ - 关于C++模板中自由函数名解析的问题

c++ - 可变参数,它们都是模板类型的特化

c++ - 检索堆内存大小及其使用统计信息等...?

c++ - 函数定义末尾的 "const"是什么意思(在上下文中)?

c++ - 从文本文件中读取具有不同分隔符的行和列

c++ - 避免数据损坏的文件结构

c++ - 尝试禁用无参数成员函数时,SFINAE 无法在 decltype() 内工作

c++ - 阴影变量问题 Qt

c++ - 一个线程写入的值是否保证在不锁定该变量的情况下被另一个线程看到?