473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Copy vector to set

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
Jul 22 '05 #1
4 9468
Xenos wrote:
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
Jul 22 '05 #2
One joyful day (Tue, 21 Sep 2004 16:46:00 -0400 to be precise), "Xenos"
<do**********@spamhate.com> decided that the Usenet community would
benefit from this remarkable comment:
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
- ma**@giallo.demon.nl

================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
================================================== =
Jul 22 '05 #3

"Mark Wright" <ma**@giallo.demon.nul> wrote in message
news:ka********************************@4ax.com...
One joyful day (Tue, 21 Sep 2004 16:46:00 -0400 to be precise), "Xenos"
<do**********@spamhate.com> decided that the Usenet community would
benefit from this remarkable comment:
I have elements of vector I need to add to a set. Can I just do (ignoringnamespace):

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 itemsin the proper order. Will set also do this, or will my above call mess upthe 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.


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
Jul 22 '05 #4
One joyful day (Wed, 22 Sep 2004 13:15:06 -0400 to be precise), "Xenos"
<do**********@spamhate.com> decided that the Usenet community would
benefit from this remarkable comment:
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
- ma**@giallo.demon.nl

================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
================================================== =
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Gunnar G | last post by:
Is there anything like vector in STL, that performes deep copy of the elements it contains? I hope this will appear in future releases of STL :)
3
by: Nudge | last post by:
I am slightly confused, so please keep that in mind if I make only little sense. Assume I have a base class: class Base { ... }; and two derived classes: class Int : public Base { ......
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
2
by: John | last post by:
I have a priority_queue that stores its elements in a vector. What is the fastest way to copy all the elements of the priority queue to this vector. Right now I use pop() to pop all the elements...
27
by: JoeC | last post by:
I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My...
9
by: hfinster | last post by:
Hello, could somebody please shed light on the following problem with g++ (4.03 and 3.3.6 as well)? Obviously, the copy constructor is not executed, if I assign the result of a function call to...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.