 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
deancoo Guest
|
Posted: Sun Feb 27, 2005 12:52 am Post subject: Some sort of append algorithm... |
|
|
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
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sun Feb 27, 2005 12:53 am Post subject: Re: Some sort of append algorithm... |
|
|
deancoo wrote:
| Quote: | 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));
| Quote: | (sorting is not necessary). I've looked at set_union and merge, but
they don't seem to do the trick.
|
Jonathan
|
|
| Back to top |
|
 |
deancoo Guest
|
Posted: Sun Feb 27, 2005 2:43 am Post subject: Re: Some sort of append algorithm... |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Sun Feb 27, 2005 4:39 am Post subject: Re: Some sort of append algorithm... |
|
|
deancoo wrote:
| Quote: | 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.
|
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
|
|
| Back to top |
|
 |
davidrubin@warpmail.net Guest
|
Posted: Sun Feb 27, 2005 4:40 am Post subject: Re: Some sort of append algorithm... |
|
|
It's in the 'iterator' header file. It's a function template in the
'std' namespace. /david
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|