STL

R

Roland groenvynck

I try to use STL to contruct two sets and make the unions of it , but
my program keeps running in a loop when the union is tried :

typedef set<int> TVSet;
void CPSetDlg::OnButton1()
{
TVSet V1, V2, V3;
V1.insert (1);
V1.insert (2);

V2.insert (1);
V2.insert (3);



set_union(V1.begin(),
V1.end() ,
V2.begin(),
V2.end(),
V3.begin()
);
}

Could someone help me?? thank you
 
R

Rob Williscroft

Roland groenvynck wrote in
in comp.lang.c++:
I try to use STL to contruct two sets and make the unions of it , but
my program keeps running in a loop when the union is tried :

typedef set<int> TVSet;
void CPSetDlg::OnButton1()
{
TVSet V1, V2, V3;
V1.insert (1);
V1.insert (2);

V2.insert (1);
V2.insert (3);



set_union(V1.begin(),
V1.end() ,
V2.begin(),
V2.end(),
V3.begin()
);
}

Could someone help me?? thank you

#include <iostream>
#include <ostream>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
using namespace std;

set< int > V1, V2, V3;
V1.insert (1);
V1.insert (2);

copy( V1.begin(), V1.end(), ostream_iterator< int >( cout, ", " ) );
cout << '\n';

V2.insert (1);
V2.insert (3);

copy( V2.begin(), V2.end(), ostream_iterator< int >( cout, ", " ) );
cout << '\n';

set_union(
V1.begin(), V1.end(),
V2.begin(), V2.end(),
ostream_iterator< int >( cout, ", " )
);
cout << '\n';

set_union(
V1.begin(), V1.end(),
V2.begin(), V2.end(),
inserter( V3, V3.end() )
);
cout << '\n';

copy( V3.begin(), V3.end(), ostream_iterator< int >( cout, ", " ) );
cout << '\n';
}

std::set_union()'s 5th paramiter needs to be an output iterator,
two examples above.

Rob.
 
R

red floyd

I try to use STL to contruct two sets and make the unions of it , but
my program keeps running in a loop when the union is tried :

typedef set<int> TVSet;
void CPSetDlg::OnButton1()
{
TVSet V1, V2, V3;
V1.insert (1);
V1.insert (2);

V2.insert (1);
V2.insert (3);



set_union(V1.begin(),
V1.end() ,
V2.begin(),
V2.end(),
V3.begin()
^^^ -- wrong. Try this instead
std::back_inserter(V3.begin())
 
K

Kevin Goodsell

red said:
[proposed set_union problem redacted]


DISCLAIMER: I don't have my copy of the library reference here. I
don't remember if std::set has a back_inserter.

No time to check right now, but it probably doesn't have a push_back()
(why would it?). You probably want std::inserter for std::set.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top