Copy vector to set

X

Xenos

I have elements of vector I need to add to a set. Can I just do (ignoring
namespace):

copy(v.begin(), v.end(), inserter(s, s.end()));

Where, v is a vector and s is a set, and both have the same value_type.

I'm worried about the iterator required by inserter, and thus insert. I
know a map will just use the iterator as a hint, and still place the items
in the proper order. Will set also do this, or will my above call mess up
the ordering?


Thanks,

DrX
 
V

Victor Bazarov

Xenos said:
I have elements of vector I need to add to a set. Can I just do (ignoring
namespace):

copy(v.begin(), v.end(), inserter(s, s.end()));

Where, v is a vector and s is a set, and both have the same value_type.

I'm worried about the iterator required by inserter, and thus insert. I
know a map will just use the iterator as a hint, and still place the items
in the proper order. Will set also do this, or will my above call mess up
the ordering?

No mess-up should happen. std::insert_iterator just causes the set to
insert. So, whatever std::set::insert does, you'll get.

V
 
M

Mark Wright

I have elements of vector I need to add to a set. Can I just do (ignoring
namespace):

copy(v.begin(), v.end(), inserter(s, s.end()));
Yes.

Where, v is a vector and s is a set, and both have the same value_type.

I'm worried about the iterator required by inserter, and thus insert. I
know a map will just use the iterator as a hint, and still place the items
in the proper order. Will set also do this, or will my above call mess up
the ordering?

Sets are ordered by definition, so it doesn't matter how or where you
insert. Also, neither map nor set will use the iterator as a hint, to do
so would corrupt the ordering, unless the iterator happened to be at the
correct place for the object to be inserted.

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 
X

Xenos

Mark Wright said:
Sets are ordered by definition, so it doesn't matter how or where you
insert. Also, neither map nor set will use the iterator as a hint, to do
so would corrupt the ordering, unless the iterator happened to be at the
correct place for the object to be inserted.

Stroustrup's Book (the 3rd edition) says the iterator is used as a hint of
where to start searching for the correct point of insertion, it doesn't
corrupt the ordering. Whether it really does use it as a hint or not by the
standard, I don't really care. I just care that set will also not corrupt
the ordering, which you and Mr. B. says it won't. So, Thanks!

DrX
 
M

Mark Wright

Stroustrup's Book (the 3rd edition) says the iterator is used as a hint of
where to start searching for the correct point of insertion, it doesn't
corrupt the ordering.

Ah, sorry, I didn't know that (even though I've had that book for
several years!).

Mark Wright
- (e-mail address removed)

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
===================================================
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top