简体   繁体   English

双端队列向量的C ++错误消除元素

[英]C++ error erasing element of vector of deque

I have the following code: 我有以下代码:

deque<int> assigned_g_col_ids;
vector < deque<int> > assigned_g(MAX_P);
deque<int>::iterator it;

int distribute_gifts(int collegue) {
int position = 0;
for (int i = 0; i < number_of_presents; i++) {
    if (present_preferences[collegue][i] && available_gifts[i] > 0) {
        assigned_gifts[i].push_back(collegue);
        available_gifts[i]--;
        return 1;
    } else if (present_preferences[collegue][i] && !(available_gifts[i] > 0) && visited_gifts[i] == 0) {        
        visited_gifts[i] = 1; 
        assigned_gift_collegues_ids = assigned_gifts[i];
        for (it = assigned_gift_collegues_ids.begin(); it != assigned_gift_collegues_ids.end(); it++) {
            if (distribute_gifts(*it)) {
                assigned_gifts[i].erase(assigned_gift_collegues_ids.begin());
                assigned_gifts[i].push_back(collegue);

                return 1;
            }
            position+=1;
        }
    }
}
return 0;

} }

I'm getting memory error at assigned_g[i].erase(assigned_g_col_ids.begin() + position); 我在assigned_g[i].erase(assigned_g_col_ids.begin() + position);处遇到内存错误assigned_g[i].erase(assigned_g_col_ids.begin() + position);

How can I erase the element pointed by it from the deque if the return value of distribute_g(*it) is 1? 如果distribute_g(*it)的返回值为1,如何从deque删除it指向的元素?

You make a copy of the deque in this statement: 您可以在以下语句中复制双端队列:

assigned_g_col_ids = assigned_g[i]

and then you use the erase of the original deque with and iterator from the copy 然后使用副本中的和迭代器擦除原始双端队列

assigned_g[i].erase(assigned_g_col_ids.begin() + position);


Your code is really hard to read. 您的代码确实很难阅读。 Eg you should declare variables where you use them, not at the beginning of the code as globals! 例如,您应该在使用变量的地方声明变量,而不是在代码开头将其声明为全局变量! (And I did not spot the recursive call either. As pointed out by David Hammen, do not use global variables as locals in recursive functions! And in any function!) (而且我也没有发现递归调用。正如David Hammen指出的那样,在递归函数中以及任何函数中,请勿将全局变量用作局部变量!)

Also, using the standard algorithms as suggested in the comments can improve readability. 同样,使用注释中建议的标准算法可以提高可读性。

And I still do not understand what your code is trying to do. 而且我仍然不明白您的代码正在尝试做什么。

There are a number of things wrong here. 这里有很多错误。 A minor problem: It's best to adopt the habit of using prefix rather than suffix autoincrement and autodecrement (eg, ++it rather than it++ ). 一个小问题:最好养成使用前缀而不是后缀自动递增和自动递减的习惯(例如++it而不是it++ )。

A much bigger problem is that you have declared deque<int>::iterator it as a file scope variable. 一个更大的问题是,您已将deque<int>::iterator it声明为文件范围变量。 You are using that variable in multiple contexts because your function distribute_g is recursive. 您正在多个上下文中使用该变量,因为您的函数distribute_g是递归的。 Those recursive calls will do who knows what to that global iterator. 那些递归调用将执行谁知道该全局迭代器要执行的操作。 The iterator should be local to the for loop in which it is used. 迭代器应该在使用它的for循环本地。

A related problem is that the iterator is invalidated by calling erase . 一个相关的问题是,迭代器会通过调用erase而失效。 Your code is immediately returns after calling erase , so it would appear that this is safe. 您的代码会在调用erase后立即返回,因此看来这是安全的。 However, you are calling distribute_g recursively. 但是,您正在递归调用distribute_g You need to reset the iterator after that recursive call to distribute . 您需要在递归调用后重置迭代器以进行distribute

Finally, what is the rationale for the recursive calls? 最后,递归调用的原理是什么?

Update 更新资料
Your updated code is illegal (and so was your original code). 您更新的代码是非法的(原始代码也是非法的)。

Here's the crux of your problem: 这是您问题的症结所在:

assigned_gift_collegues_ids = assigned_gifts[i];
...
assigned_gifts[i].erase(assigned_gift_collegues_ids.begin());

That assignment to assigned_gift_collegues_ids makes a copy of assigned_gifts[i] . 这分配assigned_gift_collegues_ids使得副本assigned_gifts[i] The iterator returned by assigned_gift_collegues_ids.begin() points to the start of the contents of that copy. assigned_gift_collegues_ids.begin()返回的迭代器指向该副本内容的开头。 It is illegal to use this iterator as an argument to assigned_gifts[i].erase() . 将此迭代器用作assigned_gifts[i].erase()的参数是非法的。

You need to rethink your design and your use of file scope variables. 您需要重新考虑设计和文件作用域变量的使用。 This latter point is particularly so within the context of recursive functions. 后一点在递归函数的上下文中尤其如此。

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

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