STL list :: splice-迭代器有效性 [英] STL list::splice - iterator validity

查看:102
本文介绍了STL list :: splice-迭代器有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读" list :: splice 的工作方式以及我不明白:

I'm reading how "list::splice" works and I don't understand something:

  mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
                                // mylist2 (empty)
                                // "it" still points to 2 (the 5th element)

  mylist2.splice (mylist2.begin(),mylist1, it);
                                // mylist1: 1 10 20 30 3 4
                                // mylist2: 2
                                // "it" is now invalid.
  it = mylist1.begin();
  std::advance(it,3);           // "it" points now to 30

  mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
                                // mylist1: 30 3 4 1 10 20

在第一个和第三个接头中, it 迭代器仍然有效,但是为什么在第二个接头中无效?

in the first and third splice the it iterator is still valid, but why isn't it in the second splice?

根据文档:

迭代器有效性

Iterator validity

在迭代器,指针和引用上没有更改 与调用之前的容器有关. 迭代器,指针和 引用转移元素的引用继续引用 相同的元素,但是迭代器现在将其迭代到容器中 元素已转移到.

No changes on the iterators, pointers and references related to the container before the call. The iterators, pointers and references that referred to transferred elements keep referring to those same elements, but iterators now iterate into the container the elements have been transferred to.

因此它应该仍然有效

推荐答案

这只是一个猜测,但他们可能已经写道,这意味着it现在无效",因为它不再是有效的迭代器的mylist1,而是成为mylist2的有效迭代器.

It's only a guess, but they might have written that to imply that it is now "invalid" in the sense that it is no longer a valid iterator of mylist1, but instead becomes a valid iterator of mylist2.

但是,我想您已经知道,它是有效的迭代器,因此其措辞具有误导性.不过,您需要注意,因为这意味着在进行第二次拼接操作之后,您将无法再执行以下操作:

But still, and I guess you already knew that, it is a valid iterator, so the wording is misleading. You need to be careful, though, as it means that after the second splice-operation, for example, you can no longer do:

std::distance( mylist1.begin(), it );

但需要使用

std::distance( mylist2.begin(), it );

因为第一个是非法的.

该标准明确定义了以下方式:

The standard clearly defines it that way in:

23.3.5.5列表操作[list.ops]

void splice(const_iterator position, list& x, const_iterator i);
void splice(const_iterator position, list&& x, const_iterator i);

23.3.5.5 list operations [list.ops]

void splice(const_iterator position, list& x, const_iterator i);
void splice(const_iterator position, list&& x, const_iterator i);

7 效果:position之前从列表x中插入i指向的元素,并从x中删除该元素.如果position == iposition == ++i,结果不变.指向*i的指针和引用继续引用相同的元素,但作为*this的成员. *i的迭代器(包括i本身)继续引用同一元素,但现在表现为*this而不是x的迭代器.

7 Effects: Inserts an element pointed to by i from list x before position and removes the element from x. The result is unchanged if position == i or position == ++i. Pointers and references to *i continue to refer to this same element but as a member of *this. Iterators to *i (including i itself) continue to refer to the same element, but now behave as iterators into *this, not into x.

因此,如果您的编译器/STL使迭代器无效,那么这显然是一个错误.

So, if your compiler/STL invalidates the iterator, this is clearly a bug.

这篇关于STL list :: splice-迭代器有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆