取消引用 C++ STL列表迭代器
我正在循环遍历两个 STL 列表(L1、L2),如下所示:
list<int>::const_iterator itr1 = L1.begin();
list<int>::const_iterator itr2 = L2.begin();
for (itr1; itr1 != L1.end(); itr1++) {
if (*itr1 < *itr2) {
//some code
}
}
它编译得很好,但是当我运行它时,它显示“表达式:列表迭代器不可取消引用”
现在在课堂上我们制作了我们编写的 STL 列表的模拟版本我们自己的 STL 列表,并且我们重载了 * 运算符来取消引用迭代器。然而,显然它在这里不起作用。
我如何取消引用迭代器,或者如果 STL 列表以不同的方式执行它,它是如何执行的。我浏览了这个:
http://www.sgi.com/tech/stl/List。 html
文档,似乎没有找到任何接受成员“引用”的内容,但仍然没有看到如何引用迭代器所指向的内容,除非它是列表的第一部分或最后一部分。
有人知道这是怎么回事吗?谢谢
,这是一个粘贴箱:
I'm looping through two STL lists (L1, L2) like so:
list<int>::const_iterator itr1 = L1.begin();
list<int>::const_iterator itr2 = L2.begin();
for (itr1; itr1 != L1.end(); itr1++) {
if (*itr1 < *itr2) {
//some code
}
}
It compiles fine but when I run it, it says "Expression: list iterator not dereferencable"
Now in class we made a mock version of the STL list where we wrote our own STL list and we had overloaded the *operator to dereference an iterator. However, obviously it's not working here.
How can I dereference an iterator, or if STL list does it differently, how does it do it. I looked through this:
http://www.sgi.com/tech/stl/List.html
documentation and didn't seem to find anything accept the member "reference" but still did not see how to reference what an iterator is pointing to, unless it's the first or last part of the list.
Anyone know what's going on here? Thank you
here is a pastebin:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个(特定于实现的)消息向我表明您取消引用了无效的迭代器。这与语法/编译时语义无关,因此您的编译器没有抱怨也就不足为奇了。但请注意,迭代器确实具有运行时语义:在这种情况下,我打赌代码是用空的
L2
列表调用的,因此itr2 == L2.end()< /代码>。这意味着
*itr2
会导致未定义的行为。幸运的是,这似乎会触发错误消息,而不是在你脸上爆炸。This (implementation specific) message suggests to me that you dereferenced an invalid iterator. This has nothing to do with syntax/compile-time semantics so no surprise that your compiler didn't complain. However note that iterators do have run-time semantics: in this case I'd wager that the code is called with an empty
L2
list, so thatitr2 == L2.end()
. That means that*itr2
results in undefined behaviour. Luckily this seems to trigger an error-message rather than blowup in your face.我的猜测:
L2
是空的,因此L2.begin()
与L2.end()
相同。这意味着 L2.begin() 返回一个不可引用的迭代器,因此您正在调用未定义的行为。
My guess:
L2
is empty, soL2.begin()
is the same asL2.end()
.Which means
L2.begin()
is returning a non-referencable iterator and you are thus invoking undefined behavior.该代码没有检查 L2 末尾是否运行。也许添加一个对
itr2 != L2.end()
的检查。That code has no check for running off the end of L2. Maybe add a check for
itr2 != L2.end()
to that.只要标准列表迭代器在列表范围内(即 [ list.begin(), list.end() )而列表不为空,就可以取消引用。
A standard list iterator can be dereferenced as long as it's within the range of your list, i.e., [ list.begin(), list.end() ) while your list is not empty.
其他答案是正确的,由于两个错误,错误的列表迭代器被取消引用。看看你的pastebin,
这个条件是向后的:
它应该是
为了在使用它之前检查itr2是否有效。另外,第一个条件
应该是析取:
但这并不是真正必要的。
(哦,还有,您知道 set_intersection 吗,它是标准的一部分图书馆?)
The other answers are correct that a bad list iterator is being dereferenced because of two bugs. Looking at your pastebin,
This condition is backwards:
It should be
in order to check that itr2 is valid before using it. Also, the first condition
should be a disjunction:
but it's not even really necessary.
(Oh, and, are you aware of set_intersection, which is part of the standard library?)