symmetric difference program does not work

The code I attach below is for getting symmetric difference by using two sets. When I use two sets with the same size, it seems working, but when I use two sets with different size, it does not work.

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
 #include <iostream>
#include <set>
#include <iterator>

using namespace std;

template <typename T>
set<T> symDifference(const set<T>& setA, const set<T>& setB)
{
	set<T> result;

	typename set<T>::const_iterator setAiter = setA.begin(), setBiter = setB.begin();

	while(setAiter != setA.end())
	{
		if(setBiter == setB.end())
		{
			result.insert(*setAiter);
			++setAiter;
		}
		else if(*setAiter < *setBiter)
		{
			result.insert(*setAiter);
			++setAiter;
		}
		else if(*setAiter > *setBiter)
		{
			result.insert(*setBiter);
			setBiter++;
		}
		else if(*setAiter == *setBiter)
		{
			++setAiter;
			++setBiter;
		}
	}

	while(setBiter != setB.end())
	{
		result.insert(*setBiter);
		setBiter++;
	}

	return result;
}

int main() {

int arr[] = {1, 2, 4, 6, 7, 9};
int arrSize = sizeof(arr)/sizeof(int);
set<int> aset(arr,arr+arrSize);

int array[] = {2, 3, 4, 5, 6, 10};
int arraySize = sizeof(array)/sizeof(int);
set<int> bset(array,array+arrSize);

set<int>cset = symDifference(aset,bset);
for(set<int>::iterator iter = cset.begin(); iter != cset.end(); iter++)
{
	cout << *iter << ' ';
}
cout << endl << endl;


}
> but when I use two sets with different size, it does not work.
saying "it does not work" is as good as nothing. Be more descriptive.
Also, if you are having issues with a particular case, then perhaps you should consider to post that particular case.


Compare lines 50--51 against lines 54--55
Yeah its output missed the last element if the size of arrays is not the same. I figured the problem, I needed to use

 
set<int> bset(array,array+arraySize);


I appreciate it.
Topic archived. No new replies allowed.