Some sort of append algorithm...

D

deancoo

I'm looking for an STL algorithm similar to 'copy', but not needing the
target container to have elements available to copy into (a 'valid range').
What I need to do is something simple like, take all the elements in
container A and append them to the end of container B (sorting is not
necessary). I've looked at set_union and merge, but they don't seem to do
the trick.

d
 
J

Jonathan Turkanis

deancoo said:
I'm looking for an STL algorithm similar to 'copy', but not needing
the target container to have elements available to copy into (a
'valid range'). What I need to do is something simple like, take all
the elements in container A and append them to the end of container B

If B is a sequence supporting push_back, you can write:

std::copy(A.begin(), A.end(), std::back_inserter(B));
(sorting is not necessary). I've looked at set_union and merge, but
they don't seem to do the trick.

d

Jonathan
 
D

deancoo

If B is a sequence supporting push_back, you can write:
std::copy(A.begin(), A.end(), std::back_inserter(B));

Jonathan

Why yes you can. Thanks Jonathan. That's going to save me allot of time
and complication. Is "back_inserter" is a member function of the sequence?
An algorithm of its own? I can't seem to find any reference to it.

d
 
J

Jonathan Turkanis

deancoo said:
Why yes you can. Thanks Jonathan. That's going to save me allot of
time and complication. Is "back_inserter" is a member function of
the sequence? An algorithm of its own? I can't seem to find any
reference to it.

I guess I should have been more explicit. Include the standard header
<iterator>. This contains the definition of the clas template
back_insert_iterator, whose instances insert elements at the end of a container,
and the helper function back_inserter, which takes a reference to a container
and returns an instance of an appropriate specialization of
back_insert_iterator.

There's also an insert_iterator and front_insert_iterator.

Jonathan
 
D

davidrubin

It's in the 'iterator' header file. It's a function template in the
'std' namespace. /david
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top