取消引用 C++ STL列表迭代器

发布于 2024-11-03 12:25:50 字数 766 浏览 0 评论 0原文

我正在循环遍历两个 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

文档,似乎没有找到任何接受成员“引用”的内容,但仍然没有看到如何引用迭代器所指向的内容,除非它是列表的第一部分或最后一部分。

有人知道这是怎么回事吗?谢谢

,这是一个粘贴箱:

http://pastebin.com/YRddqjmN

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:

http://pastebin.com/YRddqjmN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

依 靠 2024-11-10 12:25:50

这个(特定于实现的)消息向我表明您取消引用了无效的迭代器。这与语法/编译时语义无关,因此您的编译器没有抱怨也就不足为奇了。但请注意,迭代器确实具有运行时语义:在这种情况下,我打赌代码是用空的 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 that itr2 == L2.end(). That means that *itr2 results in undefined behaviour. Luckily this seems to trigger an error-message rather than blowup in your face.

没企图 2024-11-10 12:25:50

我的猜测:

L2 是空的,因此 L2.begin()L2.end() 相同。

这意味着 L2.begin() 返回一个不可引用的迭代器,因此您正在调用未定义的行为。

My guess:

L2 is empty, so L2.begin() is the same as L2.end().

Which means L2.begin() is returning a non-referencable iterator and you are thus invoking undefined behavior.

孤凫 2024-11-10 12:25:50
while ( *itr2 < *itr1 ) {
    itr2++;
}

该代码没有检查 L2 末尾是否运行。也许添加一个对 itr2 != L2.end() 的检查。

while ( *itr2 < *itr1 ) {
    itr2++;
}

That code has no check for running off the end of L2. Maybe add a check for itr2 != L2.end() to that.

橙幽之幻 2024-11-10 12:25:50

只要标准列表迭代器在列表范围内(即 [ 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.

揽月 2024-11-10 12:25:50

其他答案是正确的,由于两个错误,错误的列表迭代器被取消引用。看看你的pastebin,

这个条件是向后的:

if ( (*itr1 == *itr2) && (itr2 != L2.end()) ) {

它应该是

if ( (itr2 != L2.end()) && (*itr1 == *itr2) ) {

为了在使用它之前检查itr2是否有效。另外,第一个条件

if ( L1.empty() && L2.empty() ) {
            cout << "Returning an empty list because the two arugment lists were empty\n\n";

应该是析取:

if ( L1.empty() || L2.empty() ) {
            cout << "Returning an empty list because at least one of the two argument lists was empty\n\n";

但这并不是真正必要的。

(哦,还有,您知道 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:

if ( (*itr1 == *itr2) && (itr2 != L2.end()) ) {

It should be

if ( (itr2 != L2.end()) && (*itr1 == *itr2) ) {

in order to check that itr2 is valid before using it. Also, the first condition

if ( L1.empty() && L2.empty() ) {
            cout << "Returning an empty list because the two arugment lists were empty\n\n";

should be a disjunction:

if ( L1.empty() || L2.empty() ) {
            cout << "Returning an empty list because at least one of the two argument lists was empty\n\n";

but it's not even really necessary.

(Oh, and, are you aware of set_intersection, which is part of the standard library?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文