如何在Qt5中“合并”两个QVariantMap的内容?

时间:2016-08-06 12:24:33

标签: c++ merge qt5

我有两个QVariantMap个实例 A B

A 中,我有以下字符串:[ "key1" => "Cat", "key2" => "Dog", "key3" => "Mouse" ]。 在 B 中,我有以下字符串:[ "key1" => "Cat", "key4" => "Dog", "key3" => "Bison" ]

我想将它们合并到第三个QVariantMap实例 C 中,以便它包含以下内容:

[ "key1" => "Cat", "key2" => "Dog", "key3" => "Bison", "key4" => "Dog" ]

请注意,只有一只“猫”以及“鼠标”如何被“野牛”取代。

有没有办法在 Qt5 中执行此操作而不编写自己的实用程序函数来执行此操作?

2 个答案:

答案 0 :(得分:1)

<div class="element"></div>
<div class="element two"></div>

答案 1 :(得分:0)

QVarianMap C(A); // copy-construct C from A
for (auto i = B.constBegin(); i != B.constEnd(); ++i)
{
     C.insert(i.key(), i.value()); // add B's element to C replacing C's entry with the same key if such exists
}