🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

set function

Started by
2 comments, last by scott8 2 weeks, 3 days 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;
}

Advertisement

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

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.

Advertisement