Is there any way to swap nodes in std::list?

Iva*_*ich 5 c++

I'm implementing LRUCache, where in unordered_map I store an iterator to list. When I move the most "fresh" element to the head, I need to iterator not changed.

I need to swap exactly nodes, not values in nodes. I'm finding the way to do it.

I tried to do it with std::iter_swap, but it's just implemented as std::swap(*it_first, *it_second)

std::list<std::string> list;
list.emplace_back("first");
list.emplace_back("second");

auto it_first = list.begin();
auto it_second = ++list.begin();

std::iter_swap(it_first, it_second);

assert(list.begin() == it_second); 
Run Code Online (Sandbox Code Playgroud)

I need to swap two nodes to passed assert.

Sha*_*ger 10

splice看起来它可以通过以下方式做到这一点:

list.splice(it_first, list, it_second);
Run Code Online (Sandbox Code Playgroud)

那就是“在it_secondlist的第一个节点之前,从我自己(第二个参数)加入”。该方法保证“已移动元素的迭代器保持有效,但现在指向*this,而不是指向其他。”,这意味着原始节点本身已移动。