Wednesday, July 8, 2009

C++ code about union and intersection operation ?

i am looking for a c++ code that can do the union , intersection and etc operation.





anyone has this code ???

C++ code about union and intersection operation ?
Here's my sample program:








#include %26lt;iostream%26gt;


#include %26lt;vector%26gt;


#include %26lt;algorithm%26gt;


using namespace std;





void nPrint(vector%26lt;int%26gt;);


vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt;, vector%26lt;int%26gt;);


vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt;, vector%26lt;int%26gt;);


vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt;, vector%26lt;int%26gt;);





int main(int argn, char* argv) {


vector%26lt;int%26gt; f = vector%26lt;int%26gt;();


vector%26lt;int%26gt; s = vector%26lt;int%26gt;();


f.push_back(1);


f.push_back(2);


f.push_back(3);


f.push_back(4);


s.push_back(3);


s.push_back(4);


s.push_back(5);


s.push_back(6);





nPrint(nIntersection(f, s));


nPrint(nUnion(f, s));


nPrint(nDifference(f, s));





return 0;


}





void nPrint(vector%26lt;int%26gt; v) {


for (int i = 0; i %26lt; v.size(); ++i)


cout %26lt;%26lt; v[i] %26lt;%26lt; " ";


cout %26lt;%26lt; endl;


}





vector%26lt;int%26gt; nIntersection(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {


vector%26lt;int%26gt; v = (f.size() %26gt; s.size() ? f : s);


vector%26lt;int%26gt; res = vector%26lt;int%26gt;();


for (int i = 0; i %26lt; v.size(); ++i)


if (count(f.begin(), f.end(), v[i]) != 0 %26amp;%26amp; count(s.begin(), s.end(), v[i]) != 0)


res.push_back(v[i]);


return res;


}





vector%26lt;int%26gt; nUnion(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {


vector%26lt;int%26gt; res = vector%26lt;int%26gt;();


for (int i = 0; i %26lt; f.size(); ++i)


if (count(res.begin(), res.end(), f[i]) == 0)


res.push_back(f[i]);


for (int j = 0; j %26lt; s.size(); ++j)


if (count(res.begin(), res.end(), s[j]) == 0)


res.push_back(s[j]);


return res;


}





vector%26lt;int%26gt; nDifference(vector%26lt;int%26gt; f, vector%26lt;int%26gt; s) {


vector%26lt;int%26gt; res = vector%26lt;int%26gt;();


for (int i = 0; i %26lt; f.size(); ++i)


if (count(s.begin(), s.end(), f[i]) == 0)


res.push_back(f[i]);


return res;


}








I really hope it helps you.





Sincerely,


Nikaustr


No comments:

Post a Comment