Problem with set_union

Aug 7, 2012 at 12:37am
I'm trying to union two sets (in a vector).

setA contains a, b.
setB contains a, c.

After union, result is supposed to contain a, b, c.
However, the program is not working, it is having some kind of debug error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	vector<char> setA;
	vector<char> setB;
	vector<char> result;
	vector<char>::iterator it;

	setA.push_back('a');
	setA.push_back('b');
	setB.push_back('a');
	setB.push_back('c');

	it = set_union(setA.begin(), setA.end(), setB.begin(), setB.end(), result.begin());

	for (int i = 0; i < result.size(); i++)
	{
		cout << result[i] << " ";
	}

	system("PAUSE");
}


Does anyone know what the problem is?
Aug 7, 2012 at 1:16am
Nevermind, solved it with resizing
Aug 7, 2012 at 2:45am
Yep, an alternetive is to use an inserter iterator.
Topic archived. No new replies allowed.