Quickest way to compute the number of shared elements between two vectors

Posted by shn on Stack Overflow See other posts from Stack Overflow or by shn
Published on 2012-11-09T21:49:10Z Indexed on 2012/11/09 23:00 UTC
Read the original article Hit count: 106

Filed under:
|
|
|
|

Suppose I have two vectors of the same size vector< pair<float, NodeDataID> > v1, v2; I want to compute how many elements from both v1 and v2 have the same NodeDataID. For example if v1 = {<3.7, 22>, <2.22, 64>, <1.9, 29>, <0.8, 7>}, and v2 = {<1.66, 7>, <0.03, 9>, <5.65, 64>, <4.9, 11>}, then I want to return 2 because there are two elements from v1 and v2 that share the same NodeDataIDs: 7 and 64.

What is the quickest way to do that in C++ ?

Just for information, note that the type NodeDataIDs is defined as I use boost as:

typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> myGraph;
typedef myGraph::vertex_descriptor NodeDataID;

But it is not important since we can compare two NodeDataID using the operator == (that is, possible to do v1[i].second == v2[j].second)

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost