如何将一张地图的内容附加到另一张地图?

时间:2009-07-01 05:32:55

标签: c++ visual-c++ stl

我有两张地图:

map< string, list < string > > map1;
map< string, list < string > > map2;

我已经填充了map1,现在我想将map1内容复制到map2中。所以我只是做了:

我有一些操作,因为map1填充

 1. kiran, c:\pf\kiran.mdf, c:\pf\kiran.ldf
 2. test,  c:\pf\test.mdf, c:\pf\test.mdf

现在我必须用这个内容填充map2。并且map1填充了信息

 1. temp, c:\pf\test.mdf, c:\pf\test.ldf
 2. model, c:\model\model.mdf, c:\pf\model.ldf

现在我必须将这些内容附加到map2。我该怎么做?

6 个答案:

答案 0 :(得分:48)

map<int,int> map1;
map<int,int> map2;
map1.insert(map2.begin(), map2.end());

这将从map1的开头到结尾插入map2元素。此方法是所有STL数据结构的标准,因此您甚至可以执行类似

的操作
map<int,int> map1;
vector<pair<int,int>> vector1;
vector1.insert(map1.begin(), map1.end());

此外,指针也可以作为迭代器使用!

char str1[] = "Hello world";
string str2;
str2.insert(str1, str1+strlen(str1));

强烈建议研究STL和迭代器的神奇之处!

答案 1 :(得分:7)

您可以使用地图的使用插入方法。例如:

   std::map<int, int> map1;
    std::map<int, int> map2;

    map1[1] = 1;

    map2.insert(map1.begin(), map1.end());
    map1.clear();

    map1[2] =2;
    map2.insert(map1.begin(), map1.end());

答案 2 :(得分:3)

根据您的目的,您可以采取以下几种方式:

  1. 使用复制构造函数:

    map< string, list < string > > map1;
    // fill in map1
    
    map< string, list < string > > map2(map1);
    
  2. 使用您在问题中指明的赋值运算符:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    
    // fill in map1
    
    map2 = map1;
    
  3. 手动完成所有操作:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    
    // fill in map1
    
    for (map< string, list < string > >::iterator i = map1.begin();
         i <= map1.end(); ++i) {
      map2[i.first()] = i.second();
    }
    
  4. 听起来像(1)就是你想要的。

答案 3 :(得分:0)

我想你想要这个:

mapb.insert(mapa.begin(), mapa.end());

我相信只会跳过目标中已存在的任何键。如果要覆盖重复键的值,则必须迭代这些项,插入每个项,测试结果对并替换值。

对于“三地图”案例,您需要:

mapc.insert(mapa.begin(), mapa.end());
mapc.insert(mapb.begin(), mapb.end());

答案 4 :(得分:0)

如果您想在定义地图时插入地图,这很不错:

payload.insert({
            { "key1", "one" },
            { "key2", 2 },
        });

答案 5 :(得分:0)

由于C++17 std::map提供了merge()成员函数。它允许您从一个地图中提取内容并将其插入到另一地图中。基于您的数据的示例代码可以编写如下:

using myMap = std::map<std::string, std::list<std::string>>;

myMap  map2 = { {"kiran", {"c:\\pf\\kiran.mdf", "c:\\pf\\kiran.ldf"}},
                {"test",  {"c:\\pf\\test.mdf",  "c:\\pf\\test.mdf"}} };

myMap  map1 = { {"temp",  {"c:\\pf\\test.mdff",    "c:\\pf\\test.ldf"}},
                {"model", {"c:\\model\\model.mdf", "c:\\pf\\model.ldf"}} };

map2.merge(map1);

for (auto const &kv : map2) {
    std::cout << kv.first << " ->";
    for (auto const &str : kv.second)
        std::cout << " " << str;
    std::cout << std::endl;
}

输出:

kiran-> c:\ pf \ kiran.mdf c:\ pf \ kiran.ldf
型号-> c:\ model \ model.mdf c:\ pf \ model.ldf
temp-> c:\ pf \ test.mdff c:\ pf \ test.ldf
测试-> c:\ pf \ test.mdf c:\ pf \ test.mdf

注意:

  • 键在地图中只能存在一次(在您的示例中为kirantesttempmodel)。如果两个映射都包含相同的键,则相应的元素将不会合并到map2中并保留在map1中。
  • 如果所有元素都可以合并到map2中,那么map1将变为空。
  • merge()函数非常有效,因为既不会复制元素也不会移动元素。而是仅重定向地图节点的内部指针。

Code on Coliru

相关问题