Union of two Sets

I am having trouble understanding how the union is working out. When the union occurs, my set 1 is traversing to its last element then being added, then set 2's element 1 and element 2 are added, then it goes back to set 1's 2nd and 1st element and adds them respectively. But set 2's 3rd element is never added.

Input is: set1 = {10, 11, 12}
set2 = {20,21,22}

Output when union is done: set3 = 12 21 20 11 10

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Set.h header file
#ifndef SET_H
#define SET_H

#include "LinkedBag.h"
using std::ostream;

template<class ItemType>
class Set {
private:
	LinkedBag<ItemType> Bag;
public:
	Set(){
		
	}
	int getCurrentSize();
	void isEmpty();
	bool add(const ItemType& newEntry);
	bool remove(const ItemType& anEntry);
	bool contains(const ItemType& anEntry);
	vector<ItemType> toVector() const;
		Set<ItemType> &operator+( Set<ItemType>& setb){
			vector<ItemType> vector1 = this->toVector();
			vector<ItemType> vector2 = setb.toVector();
			Set<ItemType> *set3 = new Set<ItemType>();

			for (int i = 0; i < getCurrentSize(); i++){
				for (int j = 0; j < setb.getCurrentSize(); j++){
					//checks for matching elements in sets, or if union set already contains element.
					if (!set3->contains(vector1[i])){
						set3->add(vector1[i]);
					}
					else if (!set3->contains(vector2[j]))
						set3->add(vector2[j]);
				}
			}
			return *set3;
		}

		Set<ItemType> &operator-( Set<ItemType>& setb){
			vector<ItemType> vector1 = this->toVector();
			vector<ItemType> vector2 = setb.toVector();
			Set<ItemType> *set3 = new Set<ItemType>();

			for (int i = 0; i < getCurrentSize(); i++){
				for (int j = 0; j < setb.getCurrentSize(); j++){
					if (vector1[i] == vector2[j] && !set3->contains(vector1[i])){
						set3->add(vector1[i]);
					}

				}
			}
			return *set3;
		}

	friend std::ostream& operator<<(ostream &cout, Set<ItemType>& set3){
		vector<ItemType> vec3 = set3.toVector();
		for (int i = set3.getCurrentSize()-1; i >= 0; --i){
			cout << vec3[i] << " ";
		}
		cout << endl;
		return cout;
	}


};
#include "Set.cpp"
#endif

// Implementation File

#ifndef SET_CPP
#define SET_CPP

#include "Set.h"

template<class ItemType>
int Set<ItemType>::getCurrentSize()
{
	return Bag.getCurrentSize();
}
template<class ItemType>
void Set<ItemType>::isEmpty()
{
	Bag.isEmpty();
}
template<class ItemType>
bool Set<ItemType>::add(const ItemType& newEntry)
{
	return Bag.add(newEntry);
}

template<class ItemType>
bool Set<ItemType>::remove(const ItemType& anEntry)
{
	return Bag.remove(anEntry);
}
template<class ItemType>
bool Set<ItemType>::contains(const ItemType& anEntry) {

	return Bag.contains(anEntry);
}
template<class ItemType>
vector<ItemType> Set<ItemType>::toVector() const{
	return Bag.toVector();
}
#endif


//Main


#include<iostream>
#include "Set.h"

using namespace std;
int main()
{

	Set<int> set1;
	Set<int> set2;
	Set<int> set3;
	int selection;
	int element;
	do
	{

		cout << "1. add elements to set1\n";
		cout << "2. add elements to set2\n";
		cout << "3. remove an element from set1\n";
		cout << "4. remove an element from set2\n";
		cout << "5. do a union of set1 and set2 into set3\n";
		cout << "6. do an intersection of set1 and set2 into set3\n";
		cout << "7. print set1, set2, and set3\n";
		cout << "0. Quit\n";

		cin >> selection;

		switch (selection)
		{
		case 1:
			cout << "Add any number to set 1 (0 will quit the program): ";
			cin >> element;
			set1.add(element);
			break;
		case 2:
			cout << "Add any number to set 2 (0 will quit the program): ";
			cin >> element;
			set2.add(element);
			break;
		case 3:
			cout << "Remove a number within set 1: ";
			cin >> element;
			set1.remove(element);
			break;
		case 4:
			cout << "Remove a number within set 2: ";
			cin >> element;
			set2.remove(element);
			break;
		case 5:
			set3 = set1 + set2;
			break;
		case 6:
			set3 = set1 - set2;
			break;
		case 7:
			cout << "Set 1: " << set1 << endl;
			cout << "Set 2: " << set2 << endl;
			cout << "Set 3: " << set3 << endl;
			break;
		case 0:
			cout << "Exiting Program.\n";
			break;
		default:
			break;
	}

	} while (selection != 0);
	system("pause");
	return 0;
}
I don't see anything right of but your + (union) operation seems funky.

wouldn't it just be

copy = set a
for all of set b:
if setb.item not in copy, add it to copy, else do nothing

return copy


Topic archived. No new replies allowed.