Domanda

The problem I am having is that I need to print the resulting set from the union_set function using the copy function. (So I am not allowed to just put output as the last term of the set_union function). I cannot seem to get the copy function to work correctly for the lastAunionB pointer (i have to to a few more times as well). What I have currently is just my last attempt at an answer, although I tried many more things. What am I doing incorrectly with the copy function? Here is the code.

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    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};

    set<int> set2A(setA,setA+5);
    set<int> set2B(setB,setB+7);
    set<int> set2C(setC,setC+5);
    set<int> set2D(setD,setD+6);


    int AunionB[12];
    int AunionC[10];
    int BunionD[13];
    int AintersectB[12];
    int AintersectC[10];

    set<int> finalUnion;
    set<int> finalIntersection;
    set<int> final2Union;
    set<int> final2Intersection;
    set<int> final2Difference;
    ostream_iterator< int > output( cout, " " );

    int *lastAunionB;
    int *lastAunionC;
    int *lastBunionD;
    int *lastAintersectB;
    int *lastAintersectC;

    cout << "setA = ";
    copy(set2A.begin(), set2A.end(), output);
    cout << endl;

    cout << "setB = ";
    copy(set2B.begin(), set2B.end(), output);
    cout << endl;

    cout << "setC = ";
    copy(set2C.begin(), set2C.end(), output);
    cout << endl;

    cout << "setD = ";
    copy(set2D.begin(), set2D.end(), output);
    cout << endl;

    //here is where the problem lies
    cout << "AunionB = ";
    lastAunionB = set_union(setA, setA+5, setB, setB+7, AunionB);
    copy(lastAunionB, lastAunionB, output);
    cout << endl;

    /*
    cout << "AunionC = ";
    set_union(setA, setA+5, setC, setC+5, AunionC);
    cout << endl;

    cout << "BunionD = ";
    set_union(setB, setB+7, setD, setD+6, BunionD);
    cout << endl;

    cout << "AintercectB = ";
    set_intersection(setA, setA+5, setB, setB+7, AintersectB);
    cout << endl;

    cout << "AintercectC = ";
    set_intersection(setA, setA+5, setC, setC+7, AintersectC);
    cout << endl;

    */

    return 0;

}

È stato utile?

Soluzione

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

The last argument you pass to set_union will correspond to the beginning of the range of the new set constructed. The return value of set_union will be the end of this range.

You'd want lastAunionB and the other raw pointers and pretty much everything you're passing as an iterator here to be defined with the type std::set<int>::iterator and you'd want to copy over the range like this:

std::copy(AunionB, lastAunionB, output);

Altri suggerimenti

You're trying to copy from an empty range.

copy(ptr,ptr,output);

will always not print anything.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top