C++ 向量间的交集

C++ 向量间的交集,c++,c++11,C++,C++11,我有以下表格中的数据 vector<pair<unsigned,unsigned> > vecA; //here first value denotes reolution and second value denotes value. All values are of 4 bits vecA.push_back(make_pair(2,2)); vecA.push_back(make_pair(2,3)); vecA.push_back(make_pair(3,6))

我有以下表格中的数据

vector<pair<unsigned,unsigned> > vecA; //here first value denotes reolution and second value denotes value. All values are of 4 bits
vecA.push_back(make_pair(2,2)); vecA.push_back(make_pair(2,3)); vecA.push_back(make_pair(3,6)); vecA.push_back(make_pair(3,7)); vecA.push_back(make_pair(4,5));

(2,2)-> signifies that the first 2 bits of value(a 4 bit number) are 10. i.e. the value could be "1000,1001,1010,1011" in binary
(2,3)-> signifies that the first 2 bits of value(a 4 bit number) are 11 i.e. the value could be "1100,1101,1110, 1011" in binary
(3,6)-> signifies that the first 3 bits of value(a 4 bit number) are 110 i.e., the value could be "1100,1101" in binary
(3,7)-> signifies that the first 3 bits of value(a 4 bit number) are 111 i.e., the value could be "1110,1111" in binary
(4,5)-> signifies that the first 4 bits of value(a 4 bit number) are 0101 i.e., the value is "0101" in binary
交叉点应仅从vecB返回匹配值。i、 e.交叉口应返回“10,13,12,15,5”作为结果

如何在C++中有效地实现这个交集?< /P>
vector<unsigned> ans;
for(vector<pair<unsigned,unsigned> >::iterator i1=vecA.begin(), l1=vecA.end(); i1!=l1;++i1)
{
 for(vector<unsigned>::iterator i2=vecB.begin(),l2=vecB.end();i2!=l2;++i2)
{
    if(((*i2)>>(*i1).first)==(*i1).second)
         ans.push_back((*i1).second);
}
}
向量ans;
对于(向量::迭代器i1=vecA.begin(),l1=vecA.end();i1!=l1;++i1)
{
对于(向量::迭代器i2=vecB.begin(),l2=vecB.end();i2!=l2;++i2)
{
如果(((*i2)>>(*i1.first)==(*i1.second)
ans.push_back((*i1.second);
}
}

(2,2)
代表
10°
,我们不关心
??
是什么。这是半开范围
1000
1100
,又称
[2
(2,2)
代表
10°
,我们不关心
??
是什么。这是半开范围
1000
1100
,又称
[2你写过代码吗?或者你只是想让互联网帮你做功课吗?@kfsone我确实是通过从vecA中选择每个元素并将其与vecB中的每个元素进行比较来写代码的。但这显然是一种效率低下的交叉方式。我正在寻找一种有效的交叉方式:)然后你uld将其包含在您的帖子或SSCCE()中因此,任何回答的人都有一个参考框架。@kfsone我已经包括了代码:)你写过代码吗,还是你只是想让互联网为你做功课?@kfsone我确实是通过从vecA中选择每个元素并将其与vecB的每个元素进行比较来编写代码的。但这显然是一种低效的执行代码的方式交集。我正在寻找一种执行交集的有效方法:)然后您应该将其包含在您的帖子或SSCCE()中,以便为任何回答的人提供一个参考框架。@kfsone我已经包含了代码:)
i.e. (2,2) of vecA matches with "10" of vecB
(2,3) of vecA matches with "13,12,15" of vecB
(3,6) of vecA matches with "12,13" of vecB
(3,7) of vecA matches with "15" of vecB
(4,5) matches with "5" of vecB
vector<unsigned> ans;
for(vector<pair<unsigned,unsigned> >::iterator i1=vecA.begin(), l1=vecA.end(); i1!=l1;++i1)
{
 for(vector<unsigned>::iterator i2=vecB.begin(),l2=vecB.end();i2!=l2;++i2)
{
    if(((*i2)>>(*i1).first)==(*i1).second)
         ans.push_back((*i1).second);
}
}