有没有快速替换两个地图内容的方法?
在我的代码中,我有一张地图,其中包含大量数据(~100MB),我需要将所有数据从一张地图复制到另一张地图。目前我正在使用交换但据我了解,交换是一个奇特的方式进行复制。有没有办法简单地转移两个映射所使用的内存?我认为我可以用指针来做到这一点,但我希望有一种更优雅的方式。
In my code I have a map which holds a large amount of data (~100MB) I need to copy all that data from one map to another. currently I am doing this with swap but to my understanding, swap is a fancy way to do a copy. Is there a way to simply transfer the memory used by the two maps? I think that I can do this with pointers but I was hoping for a more elegant way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ISO/IEC 14882:2011 的 23.2.1 [container.requirements.general] 包含一般容器要求列表。对于所有标准容器,表达式
a.swap(b)
和swap(a, b)
必须交换a
和的内容b
以及除array
之外的所有标准容器,两者都必须具有恒定时间。这实际上意味着交换地图不能涉及复制所有地图元素。23.2.1 [container.requirements.general] of ISO/IEC 14882:2011 contains a list of general container requirements. For all standard containers the expressions
a.swap(b)
andswap(a, b)
must exchange the contents ofa
andb
and for all standard containers other thanarray
both must have constant time. This effectively means that swapping maps cannot involve copying all the map elements.除非这在分析器运行中作为瓶颈出现,否则您可能会过早进行优化。
我的编译器的
std::map::swap()
有以下注释,这表明映射交换可能非常快:(g++ 4.4.5
)Unless this came up in a profiler run as a bottleneck, you may be optimizing prematurely.
My compiler's
std::map::swap()
has the following comment, which indicates that a map swap is likely to be very fast:(
g++ 4.4.5
)