Search code examples
c++stlset-union

set_union issue while using arrays


I am trying to get union of 4 arrays using set_union. Here is the code I have so far:

int setA[5] = {2, 4, 5, 7, 8};
int setB[7] = {1, 2, 3, 4, 5, 6, 7};
int setC[5] = {2, 5, 8, 8, 15};
int setD[6] = {1, 4, 4, 6, 7, 12};

int AunionB[12];
int CunionD[11];
int finalUnion[23];

int *lastAunionB;
int *lastCunionD;

ostream_iterator<int> screen(cout, " ");

lastAunionB = set_union(setA, setA+5, setB, setB+7, AunionB);

cout << "AunionB = ";
copy(AunionB, lastAunionB, screen);
cout << endl;

lastCunionD = set_union(setC, setC+5, setD, setD+6, CunionD);

cout << "CunionD = ";
copy(CunionD, lastCunionD, screen);
cout << endl;

set_union(AunionB, AunionB+12, CunionD, CunionD+11, finalUnion);

cout << "Final Union = ";
copy(finalUnion, finalUnion+23, screen);
cout << endl;

When I ran the code, I got the following output:

AunionB = 1 2 3 4 5 6 7 8 
CunionD = 1 2 4 4 5 6 7 8 8 12 15 
Final Union = 1 2 3 4 5 6 7 2 4 4 5 6 7 8 8 12 15 52187240 1 1863041424 32767 0 0 

Therefore, the unions of setA and setB works as intended as does the union of setC and setD. However, when I try to get the union of all for sets, it doesn't work! I'm guessing the last 5 values of finalUnion are the address fields but how do I remove them? Also, the union itself is incorrect and I can't understand why.


Solution

  • The size of the AunionB and Cuniond is not 12 and 11 because:

    Elements from the second range that have an equivalent element in the first range are not copied to the resulting range.

    Try this code:

    int setA[5] = { 2, 4, 5, 7, 8 };
    int setB[7] = { 1, 2, 3, 4, 5, 6, 7 };
    int setC[5] = { 2, 5, 8, 8, 15 };
    int setD[6] = { 1, 4, 4, 6, 7, 12 };
    
    int AunionB[12];
    int CunionD[11];
    int finalUnion[23];
    
    int *lastAunionB;
    int *lastCunionD;
    
    ostream_iterator<int> screen(cout, " ");
    
    lastAunionB = set_union(setA, setA + 5, setB, setB + 7, AunionB);
    
    cout << "AunionB = ";
    copy(AunionB, lastAunionB, screen);
    cout << endl;
    
    lastCunionD = set_union(setC, setC + 5, setD, setD + 6, CunionD);
    
    cout << "CunionD = ";
    copy(CunionD, lastCunionD, screen);
    cout << endl;
    
    int *finalUnionEnd;
    finalUnionEnd = set_union(AunionB, lastAunionB, CunionD, lastCunionD, finalUnion);
    
    cout << "Final Union = ";
    copy(finalUnion, finalUnionEnd, screen);
    cout << endl;
    

    And then you got the right result:

    Final Union = 1 2 3 4 4 5 6 7 8 8 12 15