[英]STL map iterator runtime error
I need help with a weird runtime error. 我需要一个奇怪的运行时错误的帮助。 Here's the code which gives it:
这是给出它的代码:
JacobianCol &diag_index_column = J[diag_index];
JacobianColData::iterator &diagonal_element = diag_index_column.find(diag_index);
Jacobian J2 = J; //added to reveal the problem
J[diag_index].divide_by(diagonal_element);
What I want. 我想要的是。 I want to save iterator diagonal_element and pass it to a divide_by function.
我想保存iterator diagonal_element并将其传递给divide_by函数。 But when I call J variable the iterator goes down.
但是当我调用J变量时,迭代器就会失效。 The pointer to the memory remains (I've checked that in debugger) but the content of the iterator corrupts (unreferenced variable).
指向内存的指针仍然存在(我在调试器中检查过),但迭代器的内容会破坏(未引用的变量)。
What am I doing wrong? 我究竟做错了什么?
Some more code: 更多代码:
Jacobian J: 雅各比安J:
class Jacobian
{
private:
std::vector<JacobianCol> _J;
...
public:
...
JacobianCol& operator [](int i); //return _J[i];
};
JacobianCol: JacobianCol:
typedef std::map<int, Submatrix> JacobianColData;
class JacobianCol
{
private:
...
JacobianColData _col_data;
public:
...
JacobianColData::iterator &find(int key, bool need_append = false);
};
find implementation: 找到实施:
JacobianColData::iterator &JacobianCol::find(int key, bool need_append)
{
if(need_append)
this->insert(key);
JacobianColData::iterator &res = this->_col_data.find(key);
return res;
}
Your code won't even compile with a decent compiler. 您的代码甚至不会使用合适的编译器进行编译。
diagonal_element
should not be a reference, but a value. diagonal_element
不应该是引用,而是值。 You cannot initialize a reference with a temporary. 您无法使用临时初始化引用。
(Iterators have value semantics, and there are very, very few cases where you want a reference to an iterator—and then almost always as a parameter.) (迭代器具有值语义,并且很少有情况需要引用迭代器 - 然后几乎总是作为参数。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.