Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created April 2, 2017 04:57
Show Gist options
  • Select an option

  • Save rupalbarman/174daacc2f0fdd8511cfd3a4e5386cb2 to your computer and use it in GitHub Desktop.

Select an option

Save rupalbarman/174daacc2f0fdd8511cfd3a4e5386cb2 to your computer and use it in GitHub Desktop.
set operations in c++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int a[]= {1, 2, 90, 43, 23};
int b[]= {1, 90, 2, 43, 99};
vector<int> v(10);
vector<int>::iterator it1, it2;
sort(a, a+5); //sort required
sort(b, b+5);
it1= set_difference(a, a+5, b, b+5, v.begin()); //a-b
v.resize(it1- v.begin());
for(auto i= v.begin(); i!= v.end(); i++) {
cout<<*i<<" "; // 23
}
cout<<endl;
it2= set_difference(b, b+5, a, a+5, v.begin()); //b-a
v.resize(it2- v.begin());
for(auto i= v.begin(); i!= v.end(); i++) {
cout<<*i<<" "; // 99
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int a[]= {1, 2, 90, 43, 23};
int b[]= {1, 90, 2, 43, 99};
vector<int> v(10);
vector<int>::iterator it;
sort(a, a+5); //sort required
sort(b, b+5);
it= set_intersection(a, a+5, b, b+5, v.begin());
v.resize(it- v.begin());
for(auto i= v.begin(); i!= v.end(); i++) {
cout<<*i<<" "; // 1, 2, 43, 90
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int a[]= {1, 2, 90, 43, 23};
int b[]= {1, 90, 2, 43, 99};
vector<int> v(10);
vector<int>::iterator it;
sort(a, a+5); //sort required
sort(b, b+5);
it= set_union(a, a+5, b, b+5, v.begin()); //a-b
v.resize(it- v.begin());
for(auto i= v.begin(); i!= v.end(); i++) {
cout<<*i<<" "; // 1, 2, 3, 43, 90, 99
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment