如何合并两个 STL 地图?
如何将两张 STL 地图合并为一张?它们都具有相同的键和值类型(map
)。如果按键重叠,我想优先选择其中一张地图。
How can I merge two STL maps into one? They both have the same key and value types (map<string, string>
). If there is an overlap of the keys, I would like to give preference to one of the maps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您想保留
mapA
中的元素,并合并mapB
中mapA
中没有键的元素:将执行您想要的操作, 我认为。
(编辑:如果您使用的是 C++17 或更高版本,请考虑以下答案:https://stackoverflow.com/a/56594603/ 118150)
工作示例:
输出:
Assuming you want to preserve the elements in
mapA
, and merge elements inmapB
for which there is no key inmapA
:will do what you want, I think.
(EDIT: If you are using C++17 or newer, consider this answer: https://stackoverflow.com/a/56594603/118150)
Working example:
output:
如果要将条目从一个映射复制到另一个映射,可以使用
std::map
的insert
:但请注意,
insert
不如果元素的键已在 targetMap 中,则更新元素;这些项目将保持原样。要覆盖元素,您必须显式复制,例如:如果您不介意丢失
sourceMap
中的数据,实现复制和覆盖的另一种方法是insert 将目标转换为源,并
std::swap
结果:交换后,
sourceMap
将包含targetMap
的旧数据,并且 < code>targetMap 将是两个映射的合并,优先考虑sourceMap
的条目。If you want to copy entries from one map to another, you can use
std::map
'sinsert
:But note that
insert
does not update elements if their key is already in targetMap; those items will be left as-is. To overwrite elements, you will have to copy explicitly, e.g.:If you don't mind losing the data in
sourceMap
, another way to achieve a copy-and-overwrite is toinsert
the target into the source andstd::swap
the results:After swapping,
sourceMap
will containtargetMap
's old data, andtargetMap
will be a merge of the two maps, with preference forsourceMap
's entries.C++17
正如 John Perry 的回答中提到的,自从 C++17
std::map
提供了merge()
成员函数。merge()
函数为目标地图生成与 jkerian 的解决方案 基于使用的相同结果insert()
,如您所见下面的例子是我从 jkerian 借来的。我刚刚使用一些 C++11 和 C++17 功能更新了代码(例如使用
类型别名,基于范围的 for 循环 与 结构化绑定,以及 列表初始化):输出:
正如您所看到的,当键重叠时,
merge()
也会优先考虑目标映射foo
。如果你想反过来,那么你必须调用 bar.merge(foo); 。但是,对于源映射所发生的情况,使用
insert()
和merge()
之间存在差异。insert()
函数将新条目添加到目标映射,而merge()
函数将条目从源映射移动。这意味着对于上面的示例,insert()
不会更改bar
,但merge()
会删除4:40
code> 来自bar
,这样bar
中就只剩下2:20
和3:30
。注意:为了简洁起见,我重复使用了 jkerian 中的示例,该示例使用
map
,但merge()
也适用于您的map
。Coliru 上的代码
C++17
As mentioned in John Perry's answer, since C++17
std::map
provides amerge()
member function. Themerge()
function produces the same result for the target map as jkerian's solution based on usinginsert()
, as you can see from the following example, which I borrowed from jkerian. I just updated the code with some C++11 and C++17 features (such asusing
type alias, range-based for loop with structured binding, and list initialization):Output:
As you can see,
merge()
also gives priority to the target mapfoo
when keys overlap. If you want to have it the other way round, then you have to callbar.merge(foo);
.However, there is a difference between using
insert()
andmerge()
regarding what happens to the source map. Theinsert()
functions adds new entries to the target map, whilemerge()
moves entries over from the source map. This means for the example above, thatinsert()
does not alterbar
, butmerge()
removes4:40
frombar
, so that only2:20
and3:30
remain inbar
.Note: I reused the example from jkerian which uses
map<int, int>
for the sake of brevity, butmerge()
also works for yourmap<string, string>
.Code on Coliru
请注意,从 C++17 开始,有一个
merge()< /code>
地图方法。
Notice that, since C++17, there is a
merge()
method for maps.根据 ISO/IEC 14882:2003,第 23.1.2 节,表 69,表达式 a.insert(i,j):
由于 std::map 必须遵循此限制,因此如果您想优先考虑一个映射中的“值”而不是另一个映射,则应将其插入其中。例如,
如果 goodKeys 和 betterKeys 中有任何等效的键,则 betterKeys 的“值”将被保留。
According to ISO/IEC 14882:2003, section 23.1.2, Table 69, expression a.insert(i,j):
Since that std::map must follow this restriction, if you'd like to give preference to "values" from one map over another you should insert into it. For example,
So if there are any equivalent keys in goodKeys and betterKeys, "values" of the betterKeys will be preserved.