gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:19:44 26 4
gpt4 key购买 nike

我真的是 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));}

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

最佳答案

你这里有问题:

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

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

关于c++ - libc++abi.dylib:以 std::length_error 类型的未捕获异常终止: vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938034/

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