The following code is my attempt to form the union of the two element set {2,3} with the empty set {}. I expect that the resulting container (in this case, a list) should have size 2.
However, when I run the code, I get that the size of the union is 0 or 3, depending on which of the two indicated locations for the declaration of the variable united
. Neither of these results is what I expected and they clearly can't both be correct.
What am I missing here?
#include <list>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
//list<int> united; // resulting output is 3
int d1[] = {2,3};
set<int> dom1(d1, d1+2);
set<int> dom2;
list<int> united; // resulting output is 0
set_union(dom1.begin(), dom1.end(), dom2.begin(), dom2.end(), united.begin());
cout << united.size();
return 0;
}