Does std::set have an efficient copy?

J

Jason Heyes

I want to assign one std::set object to another without worrying about
performance. Do implementations std::set use data sharing? Thanks.
 
J

John Harrison

Jason Heyes said:
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
 
P

Peter Koch Larsen

Jason Heyes said:
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
 
I

Ivan Vecerina

John Harrison said:
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
 
J

Jason Heyes

Peter Koch Larsen said:
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.
 
P

Peter Koch Larsen

Jason Heyes said:
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
 
J

Jason Heyes

Peter Koch Larsen said:
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?
 
M

Michiel Salters

Jason Heyes said:
Peter Koch Larsen said:
Jason Heyes said:
news:[email protected]... [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
 

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

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top