简体   繁体   中英

C++ Union , Intersection , Difference

So I'm working on a code to find the Union, Intersection and Difference between two arrays. I'm done with the Union and Intersection but i just can't figure out the difference(A - B) ,For example A={1,3,6,9,7,9} , B={2,3,-2,9} i want to get A - B = { 1,6,7,0} .

I don't want to use any other library except iostream.

This is my code so far.

/* Prints union of A[] and B[]
   SizeA is the number of elements in A[]
   SizeB is the number of elements in B[] */

cout << "\nUnion of A and B = "; //
i = 0; j = 0;
while (i < SizeA && j < SizeB)
{
    if (A[i] < B[j])
        cout << A[i++] << " ";

    else if (B[j] < A[i])
        cout << B[j++] << " ";

    else
    {
        cout << B[j++] << " ";
        i++;
    }
}
/* Print remaining elements of the larger array */
while (i < SizeA)
    cout << A[i++] << " ";

while (j < SizeB)
    cout << B[j++] << " ";



cout << "\nIntersection of A and B = ";
for (i = 0; i < SizeA; i++) //for loop to calculate the intersection of A and B.
{
    for (j = 0; j < SizeB; j++)
    {
        if (A[i] == B[j])
        {
            cout << A[i] << " ";
        }
    }
}

This is very bad practice because it's not general, not using a clean function and using plain old arrays but I assumed that you are beginner and have to do it in this way

#include <iostream>

int main()
{
    int A [] ={1,3,6,9,7}, Asz =  5, B [] ={2,3,-2,9}, Bsz =  4;
    std::cout << "\nThe difference is {";
    for( int i = 0; i < Asz; ++i){
        int temp = A[i];
        bool notFound = true;
        for(int j = 0; j < Bsz; ++j){
            if(temp == B[j]) notFound = false;
        }
        if(notFound)
        {
            if(i < Asz - 1) std::cout << temp << ", ";
            else std::cout << temp ;
        }
    }
    std::cout << "}";
}

The way I prefer

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
    std::vector<int> A = {1,3,6,9,7, 0} , B={2,3,-2,9}, C;

    std::sort(A.begin(), A.end());
    std::sort(B.begin(), B.end());

    std::set_difference(A.cbegin(), A.cend(), B.cbegin(), B.cend(), std::back_inserter(C), std::less<int>{});

    for(auto const & el : C)
        std::cout << el << ", ";
}

If you could save the intersection, you could then just check each element of A and B against the elements of the intersection. Otherwise, you need two for-loops:

cout << "\nDifference of A and B = ";
for (i = 0; i < SizeA; i++) //for loop to calculate the intersection of A and B.
{
    bool found = false
    for (j = 0; j < SizeB; j++)
    {
        if (A[i] == B[j])
        {
            found = true;
        }
    }
    if (!found) {
       cout<<A[i]<<" ";
    }
}
for (i = 0; i < SizeB; i++)
{
    bool found = false
    for (j = 0; j < SizeA; j++)
    {
        if (B[i] == A[j])
        {
            found = true;
        }
    }
    if (!found) {
       cout<<B[i]<<" ";
    }
}

Edit: Never mind, i got the definition of difference wrong (thought it was (AB) + (BA)), so only the first for-loop is needed, and there's no point saving the intersection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM