1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <vector>
using namespace std;
int Is_in( string n,vector<string> s)
{
for (int i = 0;i < s.size();i++)
if (n==s[i]) return 1;
return 0;
}
void add_to_set(string n,vector<string>&s)
{
int b=0;
b= Is_in(n,s);
if (b ==1) return;
s.push_back (n);
}
void setunion(vector<string>s,vector<string>t,vector<string> &u)
{
int b=0;
string k="";
u.resize(s.size());
for (int i = 0;i<s.size();i++)
u[i]=s[i];
for (int i=0;i<t.size();i++)
{
k=t[i];
b=Is_in(k,u) ;
if (b == 0)
add_to_set(t[i],u);
}
}
void setintersect(vector<string> s,vector<string>t,vector<string>&u)
{
u.resize(0);
string k="";
int b =0;
for (int i=0;i<s.size();i++)
{
k=s[i];
b=Is_in(k,t);
if (b==1) add_to_set(k,u);
}
}
int main()
{
vector<string> u;
vector<string> currentUser = {"michael" , "john" , "jordan", "xander", "hard"} ;
vector<string> newUser = {"jordan", "michelle", "john", "jake", "james"};
setunion(currentUser,newUser,u);
for (int i=0;i<u.size();i++)
cout<<u[i]<<endl;
cout<<endl<<"duplicates:"<<endl;
setintersect(currentUser,newUser,u);
for (int i=0;i<u.size();i++)
cout<<u[i]<<endl;
cout <<endl<<"Press return to end . . ."<<endl;
cin.get();
}
| |