仅使用键作为比较器来使两个映射相交
我有两个地图,我想获得仅使用键作为比较器的两个地图的交集,同时对常见元素的值(例如+/-)进行简单的数学运算
:
map<int, double> m1,m2;
m1[1] = 1.1;
m1[2] = 2.2
m2[2] = 0.1;
m2[4] = 3.3;
交集后我将得到 m3如果我使用减法运算符,它将有对 : (2, 2.1) 。
使用算法库来实现这一点的有效方法是什么?
谢谢。
I have two maps and I would like to get map which is intersection of two using only key as a comparator and in the same time do simple math operation on values of common elements such as +/-
for example :
map<int, double> m1,m2;
m1[1] = 1.1;
m1[2] = 2.2
m2[2] = 0.1;
m2[4] = 3.3;
after intersection i will get m3 which will have pair : (2, 2.1) if I use subtraction operator.
What would be the efficient way to do it using algorithm library?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们想在这个函数中执行什么操作?
然后我们必须考虑除了 std::map 之外还有哪些其他容器适合此模型。您是否想要包含多重映射并使用相同的键组合所有元素(假设不是)?映射不必排序,因此哈希映射也应该可以工作。不幸的是,STL 概念中不存在包含映射和散列映射但不包含多重映射的概念。因此,让我们组成一个:唯一对关联容器。我们的函数应该适用于这两种类型。
现在,如果您担心 std::map 上的 merge_upacs 的运行时间,您可能希望利用映射已排序的事实,并迭代 c1 和 c2。但这是解决问题的最通用的代码。
What operations do we want to perform in this function?
Then we have to consider what other containers besides std::map fit this model. Do you want to include multimaps and combine all elements with the same key (let's assume not)? The map doesn't have to be sorted, so hashmaps should work, too. Unfortunately, there is no STL concept that include maps and hashmaps but not multimaps. So let's make one up: Unique Pair Associative Container. Our function should work on both types.
Now, if you are concerned about the running time of combine_upacs on std::map, you may want to take advantage of the fact that maps are sorted, and iterate through both c1 and c2. But this is the most general code that solves the problem.
你可以分两步完成。第一个是找到交集,可以使用 set_intersection 算法来完成。第二步将使用 transform 算法使用提供的函子进行数学运算。
我发现
set_intersection
不允许提供访问函子,因此在下面的代码中有一个惰性替换。我写了一个示例函子,可以为您做减法。我相信您可以轻松编写所有其他函子。您还可以编写与 set_intersection 相同的模板函数,但允许提供将取消引用迭代器的函子。You could do it in two steps. First one is to find intersection, it could be done using set_intersection algorithm. Second step will be using transform algorithm to do math operations using supplied functor.
I've found that
set_intersection
doesn't allow to supply access functor, so in the following code there is a lazy replacement for it. I wrote a sample functor that will do subtraction for you. I believe that you can easily write all other functors. Also you can write template function that will do the same asset_intersection
do, but with allowing to supply functor that will dereference iterator.我不知道执行此操作的标准算法,但编写一个很简单。这适用于有序和无序地图。
I don't know of a standard algorithm to do this, but it is simple to write one. This will work for both ordered and un-ordered maps.