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
|
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
class A{
public:
int n;
A(const A& rhs) {
n = rhs.n;
std::cout << "Copy\n";
}
A(int integer){
std::cout << "Constructor\n";
n = integer;
}
~A(){
std::cout << "Destructor\n";
}
bool operator < (A& a){
if (n < a.n){
return true;
}
else{
return false;
}
}
};
int main(){
std::vector<A> a1 = {1, 2, 3, 4, 5, 7};
cout << "built a1\n";
std::vector<A> a2 = {2, 4, 5, 6, 7, 8};
cout << "built a2\n";
if(a1[0] < a2[0]){
std::cout << "a1[0] < a2[0]\n";
}
cout << "built vectors\n";
std::vector<A> inter;
std::set_intersection(a1.begin(), a1.end(), a2.begin(), a2.end(), std::back_inserter(inter));
cout << "did intersection\n";
a1.erase(std::set_difference(a1.begin(), a1.end(), inter.begin(), inter.end(), a1.begin()), a1.end());
cout << "erased from a1\n";
a2.erase(std::set_difference(a2.begin(), a2.end(), inter.begin(), inter.end(), a2.begin()), a2.end());
cout << "erased from a2\n";
std::cout << "a1:";
for (auto &v : a1){
std::cout << v.n << ",";
}
std::cout << "\na2:";
for (auto &v : a2){
std::cout << v.n << ",";
}
return 0;
}
|