小编Iva*_*ich的帖子

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

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 …

c++

5
推荐指数
1
解决办法
107
查看次数

标签 统计

c++ ×1