gpt4 book ai didi

c++ - 我可以在 std::list 中移动元素而不会使迭代器或引用失效,但是如何呢?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:57 25 4
gpt4 key购买 nike

来自 cppreference article on std::list :

Addition, removal and moving the elements within the list or across several lists does not invalidate the iterators or references. An iterator is invalidated only when the corresponding element is deleted.

事实上,在对元素进行排序时就是这种情况。来自 cppreference article on std::list::sort :

This function also differs from std::sort in that it does not require the element type of the list to be swappable, preserves the values of all iterators, and performs a stable sort.

但是我怎样才能在保留所有迭代器的值的同时任意交换两个元素的位置呢?

例如,假设我有一个列表:

std::list<int> l({1, 2, 3, 4});
auto it = l.begin(), jt = ++l.begin();

现在 it 指向 1jt 指向 2。我能否重新排序此列表,使 2 位于 1 之前,但 it 仍指向 1

我能做到:

std::swap(*it, *jt);

但是,虽然 2 将出现在 1 之前,但我不会保留迭代器的值,因为显然 it 将指向 2

鉴于cppreference的forementend引用,我想应该可以实现我想要实现的目标;但是如何呢?

编辑:为了让事情更清楚:再举一个例子:

std::list<int> l({2, 1, 3, 4, 5});
auto it = l.begin(), jt = ++l.begin();

现在 it 指向 2jt 指向 1

std::list::sort 具有我正在寻找的语义:

l.sort();

现在列表的顺序是:1, 2, 3, 4, 5,但是仍然指向2jt 仍然指向 1

另一方面,std::swap(it, jt)std::swap(*it, *jt) 都没有我想要的语义.调用它们中的任何一个都会使 it 指向 1jt 指向 2。 ( ideone proof )

最佳答案

But how may I arbitrarily swap the positions of two elements while preserving the values of all iterators?

按照@cpplearner 的建议,使用 .splice() .

它可以对单个元素或范围进行操作。它还可以跨列表传输元素。

这是一个演示如何移动单个元素的简单示例。

std::list<int> list{1,2,3,4,5};

// This element will be moved
auto source = std::find(list.begin(), list.end(), 4);

// It will be inserted before this element
auto destination = std::find(list.begin(), list.end(), 2);

list.splice(destination, list, source);
// ^ ^
// | `- A list to move from
// `- A list to move to

// Prints `1 4 2 3 5`.
for (int it : list) std::cout << it << ' ';

关于c++ - 我可以在 std::list 中移动元素而不会使迭代器或引用失效,但是如何呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48330527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com