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

发布于 2024-07-26 01:23:50 字数 664 浏览 1 评论 0原文

我有以下两个地图:

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

我用以下内容填充了 map1

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

然后我将 map1 的内容复制到 map2 中,如下所示:

map2 = map1;

然后我再次使用以下新内容填充 map1

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

现在我必须将此内容附加到 map2。 我无法使用 map2 = map1;,因为这会覆盖 map2 中的现有内容。 那么,我该怎么做呢?

I have the following two maps:

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

I populated map1 with the following content:

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

Then I copied the content of map1 into map2 as follows:

map2 = map1;

Then I filled map1 again with the following new content:

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

Now I have to append this content to map2. I cannot use map2 = map1;, because this will overwrite the existing content in map2. So, how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猥︴琐丶欲为 2024-08-02 01:23:51

您可以通过多种方式执行此操作,具体取决于您想要执行的操作:

  1. 使用复制构造函数:

    地图<   字符串、列表 <   字符串>   >   地图1; 
      // 填写map1 
    
      地图<   字符串、列表 <   字符串>   >   地图2(地图1); 
      
  2. 按照问题中的指示使用赋值运算符:

    地图<   字符串、列表 <   字符串>   >   地图1; 
      地图<   字符串、列表 <   字符串>   >   地图2; 
    
      // 填写map1 
    
      地图2 = 地图1; 
      
  3. 手动完成所有操作:

    地图<   字符串、列表 <   字符串>   >   地图1; 
      地图<   字符串、列表 <   字符串>   >   地图2; 
    
      // 填写map1 
    
      for (map< string, list < string > >::iterator i = map1.begin(); 
           我<=map1.end();   ++我){ 
        map2[i.first()] = i.second(); 
      } 
      

听起来 (1) 就是您想要的。

You can do this several ways depending on what you want to do:

  1. Use the copy constructor:

    map< string, list < string > > map1;
    // fill in map1
    
    map< string, list < string > > map2(map1);
    
  2. Use the assignment operator as you indicate in the question:

    map< string, list < string > > map1;
    map< string, list < string > > map2;
    
    // fill in map1
    
    map2 = map1;
    
  3. Do it all yourself manually:

    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();
    }
    

It sounds like (1) is what you want.

定格我的天空 2024-08-02 01:23:51

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;
}

输出:

基兰 -> c:\pf\kiran.mdf c:\pf\kiran.ldf
型号-> c:\model\model.mdf c:\pf\model.ldf
温度-> c:\pf\test.mdff c:\pf\test.ldf
测试-> c:\pf\test.mdf c:\pf\test.mdf

注意:

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

Coliru 上的代码

Since C++17 std::map provides a merge() member function. It allows you to extract content from one map and insert it into another map. Example code based on your data could be written as follows:

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;
}

Output:

kiran -> c:\pf\kiran.mdf c:\pf\kiran.ldf
model -> c:\model\model.mdf c:\pf\model.ldf
temp -> c:\pf\test.mdff c:\pf\test.ldf
test -> c:\pf\test.mdf c:\pf\test.mdf

Notes:

  • A key can only exist once in a map (i.e. kiran, test, temp, model in your example). If both maps contain the same key, then the corresponding element will not be merged into map2 and remain in map1.
  • If all elements can be merged into map2, then map1 will become empty.
  • The merge() function is quite efficient, because element are neither copied nor moved. Instead, only the internal pointers of the map nodes are redirected.

Code on Coliru

情丝乱 2024-08-02 01:23:51

如果您想按照定义插入地图,这很好:

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

If you want to insert your map as you define it, this is nice:

payload.insert({
            { "key1", "one" },
            { "key2", 2 },
        });
冰雪之触 2024-08-02 01:23:50
map<int,int> map1;
map<int,int> map2;
map1.insert(map2.begin(), map2.end());

这会将 map2 从开头到结尾的元素插入到 map1 中。 此方法是所有 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 和迭代器的魔力!

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

This will insert into map1 the elements from the beginning to the end of map2. This method is standard to all STL data structure, so you could even do something like

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

Furthermore, pointers can also function as iterators!

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

Highly recommend studying the magic of the STL and iterators!

风筝有风,海豚有海 2024-08-02 01:23:50

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

   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());

You can use use insert method of the map. For example:

   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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文