C++ 如何将一张地图的内容附加到另一张地图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1067422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I append the content of one map to another map?
提问by Cute
I have the following two maps:
我有以下两张地图:
map< string, list < string > > map1;
map< string, list < string > > map2;
I populated map1
with the following content:
我填充map1
了以下内容:
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:
然后我复制map1
进去的内容map2
如下:
map2 = map1;
Then I filled map1
again with the following new content:
然后我map1
再次填充了以下新内容:
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?
现在我必须将此内容附加到map2
. 我不能使用map2 = map1;
,因为这会覆盖map2
. 那么,我该怎么做呢?
回答by Nick Lewis
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
这map1
将从 的开头到结尾插入元素map2
。此方法是所有 STL 数据结构的标准方法,因此您甚至可以执行类似的操作
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!
强烈建议学习 STL 和迭代器的神奇之处!
回答by Naveen
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());
回答by Greg
You can do this several ways depending on what you want to do:
您可以通过多种方式执行此操作,具体取决于您要执行的操作:
Use the copy constructor:
map< string, list < string > > map1; // fill in map1 map< string, list < string > > map2(map1);
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;
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(); }
使用复制构造函数:
map< string, list < string > > map1; // fill in map1 map< string, list < string > > map2(map1);
使用您在问题中指出的赋值运算符:
map< string, list < string > > map1; map< string, list < string > > map2; // fill in map1 map2 = map1;
自己手动完成所有操作:
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.
听起来像(1)就是你想要的。
回答by Tim Sylvester
I think you want this:
我想你想要这个:
mapb.insert(mapa.begin(), mapa.end());
I believe that will just skip any keys that already exist in the target. If you want to overwrite values for duplicate keys you would have to iterate over the items, insert each one, test the result pair and replace the value.
我相信这只会跳过目标中已经存在的任何键。如果要覆盖重复键的值,则必须遍历项目,插入每个项目,测试结果对并替换值。
For the "three map" case, you would need to:
对于“三图”情况,您需要:
mapc.insert(mapa.begin(), mapa.end());
mapc.insert(mapb.begin(), mapb.end());
回答by Pellet
If you want to insert your map as you define it, this is nice:
如果您想在定义地图时插入地图,这很好:
payload.insert({
{ "key1", "one" },
{ "key2", 2 },
});
回答by honk
Since C++17std::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:
由于C++17std::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;
}
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
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
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 intomap2
and remain inmap1
. - If all elements can be merged into
map2
, thenmap1
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.
- 一个键在地图中只能存在一次(即
kiran
,test
,temp
,model
在您的示例中)。如果两个地图包含了相同的密钥,那么相应的元素将不会被合并到map2
并留在map1
。 - 如果所有元素都可以合并到 中
map2
,map1
则将变为空。 - 该
merge()
功能非常有效,因为元素既不复制也不移动。相反,只有映射节点的内部指针被重定向。