[英]deque iterator out of range
I am trying to shuffle through some array-like object. 我试图通过一些类似数组的对象改组。 I want to add a value to the front, and then pop off the last value. 我想在前面添加一个值,然后弹出最后一个值。 I tried using deque as per suggestions I have seen to other posts but I am still getting errors. 我尝试根据其他帖子上看到的建议使用双端队列,但仍然出现错误。 Any ideas? 有任何想法吗? I am new to C++ and while I could try to write around this issue I would like to know where it is so I can fix it. 我是C ++的新手,虽然我可以尝试解决此问题,但我想知道它在哪里,所以可以对其进行修复。
I am getting a: cannot seek deque iterator out of range debug error from visual studio. 我得到一个:无法从Visual Studio寻求超出范围调试错误的双端队列迭代器。
The function that was causing problems: 引起问题的功能:
(I was originally using vectors) (我最初使用向量)
void vector_pusher(deque <int> &v, int new_val ) {
v.push_front(new_val);
v.erase(v.begin() + 3);
}
Here is the rest of my code (maybe it's a constuctor issue?): 这是我其余的代码(也许是构造器问题?):
#include "lock.h"
#include <deque>
#include <iostream>
using namespace std;
lock::lock(int x, int y, int z) {
is_closed = true;
current_top = 0;
comb[0] = x % MAX_VAL;
comb[1] = y % MAX_VAL;
comb[2] = z % MAX_VAL;
deque <int> trycomb = { 1, 1, 1 };
deque <char> trydir = { 'q', 'q', 'q' };
deque <int> rot = { 1,1,1 };
}
void lock::turn(int input, char direction, int rotation) {
vector_pusher(trydir, direction);
vector_pusher(trycomb, input);
vector_pusher(rot, rotation);
}
In lock.h: 在lock.h中:
public:
lock(int, int, int);
void turn(int, char, int);
\* these functions are defined elsewhere and doing fine
void new_comb(int, int, int);
void open_close();
bool lock_status() const;
*\
//entered combination
deque <int> trycomb;
// entered diections
deque <char> trydir;
//entered rotations (0 is directly too, 1 is around once)
deque <int> rot;
private:
// current correct combo
deque <int> comb = {0, 0, 0};
//top val
//this is unessesary
int current_top;
//open-ness state of lock
bool is_closed;
//needed directions
deque <char> dir = {'r', 'l', 'r'};
Thanks for the help! 谢谢您的帮助!
It looks like you're never initializing trycomb
, trydir
, and rot
in your constructor. 看来您永远不会在构造函数中初始化trycomb
, trydir
和rot
。 You're declaring local variables in the constructor with the same name, which are shadowing the member variables of your lock
class. 您要在构造函数中声明具有相同名称的局部变量,这些局部变量将遮盖lock
类的成员变量。 Your constructor should look like this: 您的构造函数应如下所示:
lock::lock(int x, int y, int z) {
is_closed = true;
current_top = 0;
comb[0] = x % MAX_VAL;
comb[1] = y % MAX_VAL;
comb[2] = z % MAX_VAL;
trycomb = { 1, 1, 1 };
trydir = { 'q', 'q', 'q' };
rot = { 1,1,1 };
}
As already mentioned you must change your constructor to 如前所述,您必须将构造函数更改为
lock::lock(int x, int y, int z) {
is_closed = true;
current_top = 0;
comb[0] = x % MAX_VAL;
comb[1] = y % MAX_VAL;
comb[2] = z % MAX_VAL;
trycomb = { 1, 1, 1 };
trydir = { 'q', 'q', 'q' };
rot = { 1,1,1 };
}
otherwise the class members will not be initialized. 否则,类成员将不会被初始化。
Secondly, you should use pop_front
if you want to remove the first element in the queue and pop_back
to remove the last element in the queue (see the documentation of deque). 其次,如果要删除队列中的第一个元素,请使用pop_front
如果要删除队列中的最后一个元素,请使用pop_back
(请参阅deque 文档 )。 This makes the code much more readable. 这使代码更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.