我想使用C ++执行多集交集 [英] I want to perform a multi-set intersection using C++

查看:102
本文介绍了我想使用C ++执行多集交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用std::set<int>和多集类std::multiset<int>来执行一些集合操作-联合,交集等.问题是我必须在两个多集之间执行交集,这样我才能得到重复的值.当我将交集与简单集(而非多集)一起使用时,交集可以正常工作

I am using std::set<int> and multi-set classes std::multiset<int> to perform some set operations - union, intersection etc. The problem is that I have to perform intersection between two multi-sets such that I also get the duplicate values. The intersection works fine when I use it with simple sets (not multi-sets) e.g.

Set1 = {1,2,3,4,5,6}
Set2 = {4,5,6,7,8,9}
然后
std :: set_intersection给我正确的结果是{4,5,6}

Set1={1,2,3,4,5,6}
Set2={4,5,6,7,8,9}
then the
std::set_intersection give me a correct result which is {4,5,6}

但是,如果我有多重设置

However, if I have a multiset

multi-set1 {1,1,2,2,3,3,4,4,5,5,6,6}
多集2 {4,4,5,5,6,6,7,7,8,8,9,9}

multi-set1{1,1,2,2,3,3,4,4,5,5,6,6}
multi-set2{4,4,5,5,6,6,7,7,8,8,9,9}

然后我再次使用std :: set_intersection,它再次给我结果{4,5,6}

and I again use the std::set_intersection it again gives me the result {4,5,6}

这是不正确的,因为实际交点是{4,4,5,5,6,6}

which is not correct, because the actual intersection is {4,4,5,5,6,6}

尽管我使用多集来保存交集的结果,但我仍然得到了错误的答案.

Although I am using a multi-set to hold the results of intersection, still I get the wrong answer.

任何人都可以告诉我如何解决此问题.

Can anyone tell me how can I solve this issue.

推荐答案

请您发布代码以检查是否存在错误? 我已经编写了像下面的代码这样的交集示例,它可以工作.

Would you please post your code to check if there are mistakes? I have coded an intersection example like the code below and it works.

multiset<int> ms1;
ms1.insert(1);
ms1.insert(1);
ms1.insert(1);
ms1.insert(2);
ms1.insert(3);

multiset<int> ms2;
ms2.insert(1);
ms2.insert(1);
ms2.insert(2);
ms2.insert(2);
ms2.insert(4);

vector<int> v(10);
set_intersection( ms1.begin(), ms1.end(), ms2.begin(), ms2.end(), v.begin() );

结果是1、1、2,这是正确的!

the result is 1, 1, 2. which is correct!

这篇关于我想使用C ++执行多集交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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