libc ++ abi.dylib:以std :: length_error类型的未捕获异常终止:vector

时间:2015-10-04 20:27:58

标签: c++ xcode vector

我是C ++的新手。我知道这可能是由于错误的内存分配,但我试图运行只显示错误的行,并没有发现任何错误......

具有错误部分的功能(t为1211200 * 7阵列):

vector<double>tmp;
vector<size_t>tmpsort;
long ctmp=1000000,c=570404,l=1211200,indt=0;
double** m = new double*[ctmp];
for(int i = 0; i < ctmp; ++i)
    m[i] = new double[7];
    double** mt = new double*[c];
    for(int i = 0; i < c; ++i)
        mt[i] = new double[7];
for (int i=0; i<l; i++) {
    if (t[i][0]!=0&&t[i][1]!=0) {
        mt[indt][0]=t[i][0];
        mt[indt][1]=t[i][1];
        mt[indt][2]=t[i][2];
        mt[indt][3]=t[i][3];
        mt[indt][4]=t[i][4];
        mt[indt][5]=t[i][5];
        mt[indt][6]=t[i][6];
        indt++;
    }
}
for (int i=0; i<c; i++) {
    tmp.push_back(pow(pow(distanceEarth(mt[i][1], mt[i][0], d[1], d[0]),2)+pow(mt[i][2]-d[2],2),0.5));
}
    tmpsort.assign(ordered(tmp).begin(), ordered(tmp).end());//signal SIGABRT
    for (int i=0; i<1000; i++) {
        m[i][0]=mt[tmpsort[i]][0];
        m[i][1]=mt[tmpsort[i]][1];
        m[i][2]=mt[tmpsort[i]][2];
        m[i][3]=mt[tmpsort[i]][3];
        m[i][4]=mt[tmpsort[i]][4];
        m[i][5]=mt[tmpsort[i]][5];
        m[i][6]=mt[tmpsort[i]][6];
    }
    c=1000;

对于有序(来自c++ sort keeping track of indices):

template <typename T>
vector<size_t> ordered(vector<T> const& values) {
vector<size_t> indices(values.size());
iota(begin(indices), end(indices), static_cast<size_t>(0));

sort(begin(indices), end(indices),[&](size_t a, size_t b) { return values[a] < values[b];});
return indices;
}

对于distanceEarth(可以返回两点之间距离的函数):

double deg2rad(double deg) {
return (deg * M_PI / 180);}
double rad2deg(double rad) {
return (rad * 180 / M_PI);}
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1d);
lon1r = deg2rad(lon1d);
lat2r = deg2rad(lat2d);
lon2r = deg2rad(lon2d);
u = sin((lat2r - lat1r)/2);
v = sin((lon2r - lon1r)/2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));}

终端将错误消息显示为标题。谁能建议我接下来要检查什么?谢谢!

1 个答案:

答案 0 :(得分:2)

你有问题:

tmpsort.assign(ordered(tmp).begin(), ordered(tmp).end());//signal SIGABRT

函数ordered按值返回。这意味着为不同的临时向量调用begin()end()

相关问题