xcode libc++abi.dylib:以 std::length_error 类型的未捕获异常终止:向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32938034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector
提问by dinex
I am really new in C++. I know it may be due to wrong memory allocation, but I have tried to run lines indicating errors solely and found nothing wrong...
我真的是 C++ 新手。我知道这可能是由于错误的内存分配造成的,但我尝试运行仅指示错误的行,但没有发现任何错误......
Function with Error part(t is 1211200*7 array):
带有错误部分的函数(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;
For ordered(from c++ sort keeping track of indices):
对于有序(来自C++ 排序跟踪索引):
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;
}
For distanceEarth(a function which can return the distance between two points):
对于 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));}
The terminal shows the error message as title. Can anyone suggest me what to check next? Thanks!
终端将错误消息显示为标题。谁能建议我接下来要检查什么?谢谢!
回答by Bo Persson
You have a problem here:
你在这里有问题:
tmpsort.assign(ordered(tmp).begin(), ordered(tmp).end());//signal SIGABRT
The function ordered
returns by value. This means that begin()
and end()
is called for different temporary vectors.
该函数ordered
按值返回。这意味着begin()
和end()
被称为不同的临时向量。