[英]Intersection of vector of pair c++
我有以下向量:
vector<unsigned> A,B1;
vector<pair<unsigned,unsigned> > B2;
我想执行(A,B1)
的交集,然后是(A,B2)
的交集。 然后我想执行两个交集结果的并集。 向量A
和B1
包含排序的无符号整数,向量B2
包含(start,end)
值对。 示例向量A
和B2
,以及它们的交集向量如下所示:
vector<unsigned> A(2,4,6,8,9,10,34,74,79,81,89,91,95);
vector<pair<unsigned,unsigned> > B2={ {2, 3}, {29, 40}, {60, 85} };
vector<unsigned> intersection; //result of intersection of A and B2 -> Procedure of performing intersection is explained below
//intersection=(2,34,74,79,81);
2
在交集,因为2
在{2,3}
。 类似地, 34
处于交集,因为34
位于{29,40}
之间。 类似地, 74
、 79
、 81
处于交集,因为它们位于B2
的最后一个元素{60,85}
的范围内。
是否有一些有效的方法可以让我获得与以下相同的结果:
(1). 交叉点A
和B1
; (2). A
和B2
交点; (3). 步骤 1 和 2 中执行的两个交集的并集(即交集(A,B1)
和(A,B2)
)
更新:
重新阅读问题后,我意识到您可能希望将所有三个操作合二为一。
我们可以通过将三个函数合二为一并返回一个元组来将外循环的迭代次数减少到一次并删除一个内循环:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <tuple>
std::tuple<std::vector<unsigned>, std::vector<unsigned>, std::vector<unsigned>>
find_everything(const std::vector<unsigned>& a,
const std::vector<std::pair<unsigned,unsigned> >& b1,
const std::vector<std::pair<unsigned,unsigned> >& b2)
{
std::vector<unsigned> r1, r2, both;
for (auto x : a)
{
auto either = false;
auto i = std::find_if(std::begin(b1),
std::end(b1),
[x](const auto& range)
{ return x >= range.first && x < range.second; });
if (i != std::end(b1)) {
either = true;
r1.push_back(x);
}
i = std::find_if(std::begin(b2),
std::end(b2),
[x](const auto& range)
{ return x >= range.first && x < range.second; });
if (i != std::end(b2)) {
either = true;
r2.push_back(x);
}
if (either) {
both.push_back(x);
}
}
return std::make_tuple(std::move(r1), std::move(r2), std::move(both));
}
int main()
{
using namespace std;
vector<unsigned> A { 2,4,6,8,9,10,34,74,79,81,89,91,95 };
vector<pair<unsigned,unsigned> > B1={ {4, 5}, {8, 10}, {90, 99} };
vector<pair<unsigned,unsigned> > B2={ {2, 3}, {29, 40}, {60, 85} };
auto results = find_everything(A, B1, B2);
const auto& r1 = std::get<0>(results);
const auto& r2 = std::get<1>(results);
const auto& both = std::get<2>(results);
copy(begin(r1), end(r1), ostream_iterator<unsigned>(cout, ", "));
cout << endl;
copy(begin(r2), end(r2), ostream_iterator<unsigned>(cout, ", "));
cout << endl;
copy(begin(both), end(both), ostream_iterator<unsigned>(cout, ", "));
cout << endl;
return 0;
}
预期成绩:
4, 8, 9, 91, 95,
2, 34, 74, 79, 81,
2, 4, 8, 9, 34, 74, 79, 81, 91, 95,
进一步的工作:
如果数据集很大,我们可以立即做出两个明显的改进:
由于 A、B1 和 B2 已排序,我们可以跟踪“当前”匹配迭代器,减少每个匹配或不匹配的搜索空间(这开始争论std::lower_bound
)
或者如果 A 明显大于 B1 和 B2,我们可以并行搜索。
玩得开心 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.