将multi_key映射转换为"normal"映射.映射连接多个键 [英] Convert a multi_key map into a "normal" map joining the multiple keys

查看:46
本文介绍了将multi_key映射转换为"normal"映射.映射连接多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将多键映射压缩"到法线映射的方法.我的多图看起来像这样:

I am looking for a way to "zip" a multikey map to a normal map. My multimap looks like this:

Key                 v a l u e s 
<string>            <string>

140,1,08,2500       1456 
140,1,10,3040       1456 
.............       ....
.............       ....
140,1,08,2500       8g   
140,1,13,3040       8g   
.............       ....

第一个和倒数第二个键相同.我的目标是合并这两行(以及所有其他重复的密钥对),以便结果是:

The first and the penultimate key are identical. My goal is to merge these two (and all other duplicate key pairs) lines so that the result is:

140,1,08,2500       14568g

属于原始两个键的值被合并.现在,我可以编写一些不错的循环,并迭代,复制和删除内容,等等,但是我想知道是否有人知道一种聪明甚至快速的方法来完成此工作.

The values belonging to the original two keys being merged. Now, I could write some nice loop and iterate and copy and delete stuff and so on, but I wonder if anyone knows a smart, maybe even fast way to get this done.

推荐答案

保持简单...

map<string, string> new_map;

for(auto it = mmap.begin(), end = mmap.end(); it != end; ++it)
{
  new_map[it->first].append(it->second);
}

为什么要使其更复杂?

这篇关于将multi_key映射转换为"normal"映射.映射连接多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆