Skip to main content
GameDev.net gamedev.net
🔒 Locked

set function

Started by pbivens67 Jun 1, 2024 at 11:43 PM 2 replies 12.6k views
Original Post
pbivens67
pbivens67

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;
}

Alberth
Alberth

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

scott8
scott8

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.