| 1 |
#include <vector> |
| 2 |
#include <iostream> |
| 3 |
using namespace std; |
| 4 |
|
| 5 |
// |
| 6 |
// setCompare |
| 7 |
// ---------- |
| 8 |
// input: two sorted vector<T>, references to return vectors. |
| 9 |
// return: 'left' will contain elements unique to lhs. |
| 10 |
// 'right' will contain elements unique to rhs. |
| 11 |
// 'same' will contain elements common to both. |
| 12 |
// |
| 13 |
template<class T> |
| 14 |
void setCompare( |
| 15 |
vector<T>& left, |
| 16 |
vector<T>& right, |
| 17 |
vector<T>& same, |
| 18 |
vector<T>& lhs, |
| 19 |
vector<T>& rhs) |
| 20 |
{ |
| 21 |
int a = 0, b = 0; |
| 22 |
|
| 23 |
while((a < lhs.size()) && (b < rhs.size())) |
| 24 |
{ |
| 25 |
if (lhs[a] < rhs[b]) |
| 26 |
{ |
| 27 |
left.push_back(lhs[a]); |
| 28 |
++a; |
| 29 |
} |
| 30 |
else if (lhs[a] == rhs[b]) |
| 31 |
{ |
| 32 |
same.push_back(lhs[a]); |
| 33 |
++a; |
| 34 |
++b; |
| 35 |
} |
| 36 |
else // lhs[a] > rhs[b] |
| 37 |
{ |
| 38 |
right.push_back(lhs[a]); |
| 39 |
++b; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
for(; a < lhs.size();++a) |
| 44 |
{ |
| 45 |
left.push_back(lhs[a]); |
| 46 |
} |
| 47 |
|
| 48 |
for(; b < rhs.size(); ++b) |
| 49 |
{ |
| 50 |
right.push_back(rhs[b]); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
int main() |
| 55 |
{ |
| 56 |
vector<int> a, b, c; |
| 57 |
a.push_back(1); |
| 58 |
a.push_back(2); |
| 59 |
a.push_back(3); |
| 60 |
a.push_back(4); |
| 61 |
a.push_back(5); |
| 62 |
a.push_back(6); |
| 63 |
|
| 64 |
b.push_back(2); |
| 65 |
b.push_back(4); |
| 66 |
b.push_back(6); |
| 67 |
|
| 68 |
c.push_back(1); |
| 69 |
c.push_back(3); |
| 70 |
c.push_back(5); |
| 71 |
|
| 72 |
vector<int> left, right, same; |
| 73 |
|
| 74 |
cout << "setCompare(a, a): " << endl; |
| 75 |
setCompare(left, right, same, a, a); |
| 76 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 77 |
left.erase(left.begin(), left.end()); |
| 78 |
right.erase(left.begin(), r |
| 79 |
|
| 80 |
cout << "setCompare(a, b): " << endl; |
| 81 |
setCompare(left, right, same, a, b); |
| 82 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 83 |
cout << "setCompare(a, c): " << endl; |
| 84 |
setCompare(left, right, same, a, c); |
| 85 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 86 |
|
| 87 |
cout << "setCompare(b, a): " << endl; |
| 88 |
setCompare(left, right, same, b, a); |
| 89 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 90 |
cout << "setCompare(b, b): " << endl; |
| 91 |
setCompare(left, right, same, b, b); |
| 92 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 93 |
cout << "setCompare(b, c): " << endl; |
| 94 |
setCompare(left, right, same, b, c); |
| 95 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 96 |
|
| 97 |
cout << "setCompare(c, a): " << endl; |
| 98 |
setCompare(left, right, same, c, a); |
| 99 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 100 |
cout << "setCompare(c, b): " << endl; |
| 101 |
setCompare(left, right, same, c, b); |
| 102 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 103 |
cout << "setCompare(c, c): " << endl; |
| 104 |
setCompare(left, right, same, c, c); |
| 105 |
cout << "left = " << left << ", right = " << right << ", same = " << same << endl; |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|