简体   繁体   English

c ++向量插入访问冲突读取位置

[英]c++ vector insert Access violation reading location

My vectors are constructed like this: 我的向量是这样构造的:

struct segment {
    float x1;
    float y1;
    float x2;
    float y2;
    float k;
    float b;
};

enum EventType {UPPER_PT = 0, LOWER_PT, INTER_PT};
struct order {
    float x;
    float y;
    vector<segment>::iterator line_id;
    vector<segment>::iterator line2_id;
    EventType type;
};

vector<segment> seg_lines;
vector<order> event_list;

After initializing event_list , I try to insert() a new element at random position like this: 初始化event_list ,我尝试在随机位置insert()一个新元素,如下所示:

order new_event;
new_event.x = 300;
new_event.y = 400;
new_event.line_id = an_iterator_in_seg_lines;
new_event.line2_id = another_iterator_in_seg_lines;
new_event.type = INTER_PT;

bool inte_inserted = false;
for(vector<order>::iterator tmp_idx = event_list.begin(); tmp_idx != event_list.end(); tmp_idx++) {
    if(a_Y_threshold > tmp_idx->y || a_Y_threshold == tmp_idx->y && an_X_threshold < tmp_idx->x) {
        event_list.insert(tmp_idx, new_event);
        inte_inserted = true;
        break;
    }
}

I can assure you that line_id and line2_id are both valid. 我可以向您保证line_idline2_id均有效。 I'm just trying to simplify my description. 我只是想简化我的描述。

In this case, it's trying to insert a new_event at the "5th position", while the size and capacity of event_list are both 10 and the event_list is full of valid order . 在这种情况下,它将尝试在“第5个位置”插入一个new_event ,而event_list的大小和容量均为10,并且event_list充满有效order

But I get such error: 但是我得到这样的错误:

Unhandled exception in ... 0xC0000005: Access violation reading location 0x00000000

I think the error code tells me that I'm using an invalid pointer. 我认为错误代码告诉我我正在使用无效的指针。 But I checked my 'Watch' view, and all iterators related (including tmp_idx ) are all valid and pointing to the right positions. 但是我检查了“观察”视图,所有相关的迭代器(包括tmp_idx )都有效并且指向正确的位置。

I try to use emplace() instead of insert() , it doesn't work. 我尝试使用emplace()而不是insert() ,它不起作用。 I tried to resize() my event_list before inserting new element, it doesn't work either. 我尝试在插入新元素之前对event_list进行resize() ,但这也不起作用。 But when I use list to construct event_list instead of vector , it works like a charm. 但是,当我使用list构造event_list而不是vector ,它就像一个魅力。

So, what may have caused this problem and what should I do if I want to stick with vector ? 那么,是什么引起了这个问题?如果我要坚持使用vector怎么办?

I'm using VS 2010. 我正在使用VS 2010。

from cplusplus 来自cplusplus

If a reallocation happens, all iterators, pointers and references related to the container are invalidated. 如果发生重新分配,则与容器相关的所有迭代器,指针和引用都将无效。

You are inserting while looping on a vector, which may invalidate your iterators. 您在循环插入向量时插入,这可能会使迭代器无效。 You could push back elements into the vector instead of inserting them, and only at the end reorder the vector. 您可以将元素推回向量中,而不是插入它们,仅在最后对向量重新排序。 Much easier and cleaner. 更轻松,更清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM