Advertisement

set function

Started by June 01, 2024 11:43 PM
2 comments, last by scott8 5 months, 3 weeks ago

I am trying to print out the union of two sets. Here is my code so far.

#include <iostream>
#include <set>

using namespace std;

set <int> set_union(set<int> a, set<int> b)
{
	a.insert(1);
	a.insert(3);
	a.insert(5);
	a.insert(7);
	a.insert(9);
	b.insert(2);
	b.insert(3);
	b.insert(5);
	b.insert(7);
	a.insert(b.begin(),b.end());
	return a;
}

set <int> intersection(set<int> a, set<int> b)
{
	a.insert(1);
	a.insert(3);
	a.insert(5);
	a.insert(7);
	a.insert(9);
	b.insert(2);
	b.insert(3);
	b.insert(5);
	b.insert(7);
}

int main()
{



	return 0;
}

Use an iterator to walk over the elements of the set, and print each value that you get.

Advertisement

Your function ‘set_union' is doing more than finding the union; it's populating both sets, a and b. This should be done before calling set_union, which would be in main.

This topic is closed to new replies.

Advertisement