error using set_union of stl with sets

#include<iostream>
#include<set>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef set<short> pos;
typedef struct tree
{
pos first,result;
}tree;
class check
{
public:
pos last;
void check_set();
};
void check::check_set()
{
tree p;
check obj;


p.first.insert(5);
p.first.insert(3);
p.first.insert(4);
p.first.insert(1);


obj.last.insert(8);
obj.last.insert(1);
obj.last.insert(7);


cout<<"structure set"<<endl;
pos::iterator it;
for(it=p.first.begin();it!=p.first.end();it++)
{
cout<<*it<<endl;
}
cout<<"class set"<<endl;
for(it=obj.last.begin();it!=obj.last.end();it++)
{
cout<<*it<<endl;
}


pos::iterator itr,itp;
cout<<" UNION of the two sets"<<endl;
itr=set_union(p.first.begin(),p.first.end(),obj.last.begin(),obj.last.end(),p.result.begin());
//ERROR is in the fifth parameter that i am using i.e. p.result.begin()

for(itp=v1.begin();itp!=itr;itp++)
{
cout<<*itp<<endl;
}
}
int main()
{
check obj;
obj.check_set();
return 0;
}

/*I am getting errors while doing the union.I think it is becoz of the fifth parameter that i am passing i.e. p.result.begin();what is the error ?? PLEASE HELP. THANK YOU.*/
[code] "Please use code tags" [/code]
http://www.cplusplus.com/forum/articles/40071/#msg216270
You can't modify the values inside the set with iterators because that could break the structure.
You need to provide an inserter
1
2
3
4
5
6
7
8
9
10
11
#include <iterator>
set_union(
  p.first.begin(),
  p.first.end(),
  obj.last.begin(),
  obj.last.end(),
  inserter( 
    p.result, 
    p.result.end() //I'm not sure what should be here
  )
);
Topic archived. No new replies allowed.