小编axx*_*xel的帖子

How to efficiently move (some) items from one std::map to another?

I have two std::map<> objects a and b and would like to move (extract + insert) some elements (nodes) from one map to the other based on some predicate p.

for (auto i = a.begin(); i != a.end(); ++i)
    if (p(*i))
        b.insert(a.extract(i))
Run Code Online (Sandbox Code Playgroud)

This code segfaults in clang. I assume the problem is the increment of i after its node has been extracted from a.

Is the right/only way to fix this by using a post-increment?, E.g.:

for …
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap c++17

8
推荐指数
1
解决办法
115
查看次数

标签 统计

c++ ×1

c++17 ×1

stdmap ×1

stl ×1