vector insert from a set

S

Steven C

vector <int> vecThe;
set <int> setThe;

vecThe.insert (setThe.begin (), setThe.end ());

This gives an error about iterators not of the right type. Is there a
way around this or do I have to set up a for loop and do
vecThe.push_back ()?

However I can do this:

setThe.insert (vecThe.begin (), vecThe.end ());
 
I

Ivan Vecerina

Steven C said:
vector <int> vecThe;
set <int> setThe;

vecThe.insert (setThe.begin (), setThe.end ());

This gives an error about iterators not of the right type. Is there a
way around this or do I have to set up a for loop and do
vecThe.push_back ()?

The vector::insert member function overload that you want
to call actually takes 3 parameters -- the first one being
the position in the vector where the insertion needs to occur:

vecThe.insert(vecThe.end(), setThe.begin(), setThe.end () );

The alternative is to use the 'assign' function, if you want
to discard any previous contents of the vector:
vecThe.assign( setThe.begin (), setThe.end ());
However I can do this:

setThe.insert (vecThe.begin (), vecThe.end ());

Yes, set is different since the insertion location
cannot be specified (value ordering is automatically
enforced).


Cheers,
Ivan
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Steven C escribió:
vector <int> vecThe;
set <int> setThe;

vecThe.insert (setThe.begin (), setThe.end ());

You must specify where to insert. For example:

vecThe.insert (vecThe.begin (), setThe.begin (), setThe.end ());

Regards.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top