Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can std::copy be used to copy to an empty container?

2 views
Skip to first unread message

JD

unread,
Jun 1, 2009, 4:28:09 PM6/1/09
to
Hi,

I have used following similar pieces of code for many years. However, I
just found out that the document says that copy should be used to copy items
to an empty container. Is copying to an empty container serious? Please
advise.

vector<int> v;
....
set<int> s;
copy(v.begin(), v.end(), inserter(s, s.begin())); // copy v to an empty
container s

Thanks.
JD


Juha Nieminen

unread,
Jun 1, 2009, 4:34:42 PM6/1/09
to
JD wrote:
> vector<int> v;
> ....
> set<int> s;
> copy(v.begin(), v.end(), inserter(s, s.begin())); // copy v to an empty
> container s

I don't see the problem there, even though I would usually just use:
s.insert(v.begin(), v.end());

There are other situation where using an inserter iterator and an
algorithm like std::copy can be useful, though.

Pete Becker

unread,
Jun 1, 2009, 8:34:17 PM6/1/09
to

Think of it as inserting into an empty container. That's what the insert
iterator does.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

0 new messages