使用std :: sort时的核心转储

时间:2016-10-08 13:36:59

标签: 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,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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,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 ()

1 个答案:

答案 0 :(得分:2)

你的比较器打破了合同。它应该符合strict weak ordering关系。

这意味着当传递相同的元素时它应该产生false,并且cmp(a,b)cmp(b,a)永远不会为同一个true和{a两次产生b {1}}

经验法则:不要将<=用于比较器;请改用<