473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Does std::set have an efficient copy?

I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.
Jul 22 '05 #1
7 3369

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.


No they don't. Use a smart pointer.

john
Jul 22 '05 #2

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.

Depends on the implementation, but most likely it will not. Why don't you
simply use a const reference? If you are changing one of the sets, you
should a "real" copy anyway.

/Peter
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.


No they don't. Use a smart pointer.

Agreed.

A sometimes valid alternative is to use the swap() member
function of std::set (or any other container):
set1.swap( set2 );

This is guaranteed to be efficiently implemented, and might allow
you to do what you want.
For example, a common C++ idiom to avoid unnecessary copies
when adding items to a container is to replace:
myQueueOfSets.push_back( aNewSet )
with:
myQueueOfSets.push_back( std::set<ItemType>() ); // add empty set
myQueueOfSets.back().swap( aNewSet );
hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #4
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Ed********************@news000.worldonline.dk ...

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.

Depends on the implementation, but most likely it will not. Why don't you
simply use a const reference? If you are changing one of the sets, you
should a "real" copy anyway.

/Peter


You don't have to copy until the set changes. There is no need for const
references if you have data sharing.
Jul 22 '05 #5

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41**********************@news.optusnet.com.au ...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Ed********************@news000.worldonline.dk ...

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.

Depends on the implementation, but most likely it will not. Why don't you
simply use a const reference? If you are changing one of the sets, you
should a "real" copy anyway.

/Peter


You don't have to copy until the set changes. There is no need for const
references if you have data sharing.

Of course. But all this fuss probably is not worthwhile, especially on
modern platforms where multithreading is part of the game.

/Peter
Jul 22 '05 #6
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:hO********************@news000.worldonline.dk ...

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41**********************@news.optusnet.com.au ...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Ed********************@news000.worldonline.dk ...

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41***********************@news.optusnet.com.a u...
I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.

Depends on the implementation, but most likely it will not. Why don't
you simply use a const reference? If you are changing one of the sets,
you should a "real" copy anyway.

/Peter


You don't have to copy until the set changes. There is no need for const
references if you have data sharing.

Of course. But all this fuss probably is not worthwhile, especially on
modern platforms where multithreading is part of the game.

/Peter


Why is multithreading important to this discussion?
Jul 22 '05 #7
"Jason Heyes" <ja********@optusnet.com.au> wrote in message news:<41***********************@news.optusnet.com. au>...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:hO********************@news000.worldonline.dk ...

"Jason Heyes" <ja********@optusnet.com.au> skrev i en meddelelse
news:41**********************@news.optusnet.com.au ...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Ed********************@news000.worldonline.dk ... [snip] You don't have to copy until the set changes. There is no need for const
references if you have data sharing.

Of course. But all this fuss probably is not worthwhile, especially on
modern platforms where multithreading is part of the game.

/Peter


Why is multithreading important to this discussion?


Because in MT environments, other threads could notice a change in the
set when a thread is doing a COW. See Herb Sutter's analysis of the
COW string class on gotw.ca or in his (M?)XC++ book.

Regards,
Michiel Salters
Jul 22 '05 #8

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

Similar topics

9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
11
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
3
by: kathy | last post by:
I have array: double a; double b; std::vector <double> vDouble; when I use: std::copy(a,a+1024,vDouble.begin());
6
by: Chris Johnson | last post by:
Greetings all. I am really stuck on this one as I can't seem to grok if I am abusing the C++ language or if I am simply using components of the C++ Standard Library incorrectly. Here is the code:...
16
by: Cory Nelson | last post by:
Does anyone know how std::set prevents duplicates using only std::less? I've tried looking through a couple of the STL implementations and their code is pretty unreadable (to allow for different...
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
3
by: none | last post by:
Hi, Consider the following piece of code: int t={1,2,3,4,5,6}; vector<intv; std::copy (t, t+sizeof(t)/sizeof(t), std::back_inserter (v)); Could someone explain me why we can pass "t" as an...
7
by: Christian Meier | last post by:
Hello Newsgroup I have a question about the find function of std::set. When I have a "std::set<int*>", why can't I call the find() function with an "const int*"? I know that my key type is...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.