我可以将std :: map的内容移动分配给另一个std :: map吗? [英] Can I move-assign a std::map's contents into another std::map?

查看:291
本文介绍了我可以将std :: map的内容移动分配给另一个std :: map吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过使用移动语义将临时std :: map temp的内容插入另一个std :: map m中,从而不会复制并重用临时值? >

比方说,有一个:

std::map<int, Data> temp;
std::map<int, Data> m;

将值从temp复制到m的一种方法是:

m.insert(temp.begin(),temp.end());

如何将元素移至m,而不是复制?

解决方案

提示: 先阅读更新!

当前的C ++ 11标准和C ++ 14草案没有提供启用该功能的成员函数.根据lavr的建议,您仍然可以写

m.insert(make_move_iterator(begin(temp)),
         make_move_iterator(end  (temp)));

这会将从源容器移动到目标容器.但是,容器节点和键都不会移动.这需要内存分配(至少用于在目标映射中创建新节点).源容器中的元素数将保持不变.复制的原因很简单:std::map的值类型为std::pair<const Key,T>.从const Key转移实际上是在复制密钥(除非有人重载了采用const Key &&Key构造函数,为此我想不出足够的理由).

如果需要将数据从一个容器移动到另一个容器,则可以考虑使用std::list而不是std::map.它具有成员函数splice ,可将元素从一个列表中移出在恒定的时间到另一个.

更新:

自C ++ 17起,就有函数 std::map::merge() 它基本上将一个std::map的所有元素放入另一个std::map中,而无需移动或复制实际元素,而仅通过重新指向内部指针即可.它与C ++ 98以来存在的std::list::splice()非常相似.

所以你可以写

m.merge( temp );

实现您的目标.这比将所有元素从一个容器复制或移动到另一个容器更为有效.

但是要当心!冲突的密钥将无法解决:对于一致的密钥,什么也不会做.

Is it possible to insert the contents of a temporary std::map temp into another std::map m by using move semantics, such that the values from the temporary are not copied and are reused?

Let's say one has:

std::map<int, Data> temp;
std::map<int, Data> m;

One way of copying values from temp into m is:

m.insert(temp.begin(),temp.end());

How can I move the temp elements into m, instead of copying?

解决方案

HINT: Read the update first!

The current C++11 standard and the C++14 draft do not provide a member function to enable this feature. As lavr suggested you can still write

m.insert(make_move_iterator(begin(temp)),
         make_move_iterator(end  (temp)));

which will move the values from the source container into the destination container. However, neither the container nodes nor the keys will be moved. This requires memory allocations (at least for the creation of the new nodes in the destination map). The number of elements in the source container will remain the same. The reason behind the copying is simple: The value type of std::map is std::pair<const Key,T>. And moving from a const Key is essentially copying the key (unless someone overloaded the Key constructor which takes a const Key &&, for which I cannot think of an adequate reason).

If you need to move data from one container to another you may consider using std::list instead of std::map. It has a member function splice which moves the elements from one list to another in constant time.

UPDATE:

Since C++17 there is the function std::map::merge() which basically puts all the elements of one std::map into another std::map without moving or copying the actual elements, but by repointing internal pointers only. It is very similar to std::list::splice() which exists since C++98.

So you may write

m.merge( temp );

to accomplish your goal. This is more efficient than copying or moving all the elements from one container to the other.

But beware! Conflicting keys won't be resolved: For coinciding keys nothing will be done.

这篇关于我可以将std :: map的内容移动分配给另一个std :: map吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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