矢量插入使程序崩溃
谁能告诉我
为什么这会使我的程序崩溃?它应该使 t 向量中的所有元素都按 (y + height) 排列。
编辑:在其中包含“插入”的行上崩溃。
void createDrawOrder(vector<Thing*> t, vector<int> *order) {
int min = t[0]->y + t[0]->height;
int max = t[0]->y + t[0]->height;
vector<int>::iterator it;
it = order->begin();
order->push_back(0);
for (int i = 1; i < (int) t.size(); i++) {
if ((t[i]->y + t[i]->height) < min) {
min = (t[i]->y + t[i]->height);
order->insert(it, i);
}
else if((t[i]->y + t[i]->height) >= min && (t[i]->y + t[i]->height) < max){
int tempsize = (int) order->size();
for (int j = 0; j < tempsize; j++){
if((t[i]->y + t[i]->height) <= (t[(*order)[j]]->y + t[(*order)[j]]->height)){
order->insert(it + j, i);
}
}
}
else if ((t[i]->y + t[i]->height) >= max) {
max = (t[i]->y + t[i]->height);
order->push_back(i);
}
}
}//end method max
Can anyone tell me
why this crashes my program? It's suppose to make it so order has all the elements in the t vector situated by (y + height).
Edit: crashes on the lines with "insert" in them.
void createDrawOrder(vector<Thing*> t, vector<int> *order) {
int min = t[0]->y + t[0]->height;
int max = t[0]->y + t[0]->height;
vector<int>::iterator it;
it = order->begin();
order->push_back(0);
for (int i = 1; i < (int) t.size(); i++) {
if ((t[i]->y + t[i]->height) < min) {
min = (t[i]->y + t[i]->height);
order->insert(it, i);
}
else if((t[i]->y + t[i]->height) >= min && (t[i]->y + t[i]->height) < max){
int tempsize = (int) order->size();
for (int j = 0; j < tempsize; j++){
if((t[i]->y + t[i]->height) <= (t[(*order)[j]]->y + t[(*order)[j]]->height)){
order->insert(it + j, i);
}
}
}
else if ((t[i]->y + t[i]->height) >= max) {
max = (t[i]->y + t[i]->height);
order->push_back(i);
}
}
}//end method max
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的迭代器
it
不保证在order->push_back(k);
之后有效,它可以重新分配向量中的元素。由于我没有看到您在任何地方实际增加it
,因此我建议在此函数中将其替换为order->begin()
的丑陋解决方案。Your iterator
it
is not guaranteed to be valid afterorder->push_back(k);
which can reallocate the elements in your vector. Since I don't see you actually incrementingit
anywhere, I'd recommend the uglier solution of replacing it withorder->begin()
in this function.