Connecting Tech Pros Worldwide Help | Site Map

Does std::set have an efficient copy?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:52 PM
Jason Heyes
Guest
 
Posts: n/a
Default 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.



  #2  
Old July 22nd, 2005, 09:52 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?


"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:41970fcd$0$27451$afc38c87@news.optusnet.com.a u...[color=blue]
>I want to assign one std::set object to another without worrying about
>performance. Do implementations std::set use data sharing? Thanks.[/color]

No they don't. Use a smart pointer.

john


  #3  
Old July 22nd, 2005, 09:52 PM
Peter Koch Larsen
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?


"Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
news:41970fcd$0$27451$afc38c87@news.optusnet.com.a u...[color=blue]
>I want to assign one std::set object to another without worrying about
>performance. Do implementations std::set use data sharing? Thanks.
>[/color]
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


  #4  
Old July 22nd, 2005, 09:52 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?

"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2vol8qF26qj93U1@uni-berlin.de...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:41970fcd$0$27451$afc38c87@news.optusnet.com.a u...[color=green]
>>I want to assign one std::set object to another without worrying about
>>performance. Do implementations std::set use data sharing? Thanks.[/color]
>
> No they don't. Use a smart pointer.[/color]
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


  #5  
Old July 22nd, 2005, 09:53 PM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?

"Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
news:EdFld.64853$Vf.3213383@news000.worldonline.dk ...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
> news:41970fcd$0$27451$afc38c87@news.optusnet.com.a u...[color=green]
>>I want to assign one std::set object to another without worrying about
>>performance. Do implementations std::set use data sharing? Thanks.
>>[/color]
> 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[/color]

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


  #6  
Old July 22nd, 2005, 09:54 PM
Peter Koch Larsen
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?


"Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
news:4197f011$0$2675$afc38c87@news.optusnet.com.au ...[color=blue]
> "Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
> news:EdFld.64853$Vf.3213383@news000.worldonline.dk ...[color=green]
>>
>> "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
>> news:41970fcd$0$27451$afc38c87@news.optusnet.com.a u...[color=darkred]
>>>I want to assign one std::set object to another without worrying about
>>>performance. Do implementations std::set use data sharing? Thanks.
>>>[/color]
>> 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[/color]
>
> You don't have to copy until the set changes. There is no need for const
> references if you have data sharing.
>[/color]
Of course. But all this fuss probably is not worthwhile, especially on
modern platforms where multithreading is part of the game.

/Peter


  #7  
Old July 22nd, 2005, 09:55 PM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?

"Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
news:hO4md.65079$Vf.3231893@news000.worldonline.dk ...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
> news:4197f011$0$2675$afc38c87@news.optusnet.com.au ...[color=green]
>> "Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
>> news:EdFld.64853$Vf.3213383@news000.worldonline.dk ...[color=darkred]
>>>
>>> "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
>>> news:41970fcd$0$27451$afc38c87@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[/color]
>>
>> You don't have to copy until the set changes. There is no need for const
>> references if you have data sharing.
>>[/color]
> Of course. But all this fuss probably is not worthwhile, especially on
> modern platforms where multithreading is part of the game.
>
> /Peter[/color]

Why is multithreading important to this discussion?


  #8  
Old July 22nd, 2005, 09:56 PM
Michiel Salters
Guest
 
Posts: n/a
Default Re: Does std::set have an efficient copy?

"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message news:<41996649$0$24379$afc38c87@news.optusnet.com. au>...[color=blue]
> "Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
> news:hO4md.65079$Vf.3231893@news000.worldonline.dk ...[color=green]
> >
> > "Jason Heyes" <jasonheyes@optusnet.com.au> skrev i en meddelelse
> > news:4197f011$0$2675$afc38c87@news.optusnet.com.au ...[color=darkred]
> >> "Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
> >> news:EdFld.64853$Vf.3213383@news000.worldonline.dk ...[/color][/color][/color]
[snip][color=blue][color=green][color=darkred]
> >> You don't have to copy until the set changes. There is no need for const
> >> references if you have data sharing.
> >>[/color]
> > Of course. But all this fuss probably is not worthwhile, especially on
> > modern platforms where multithreading is part of the game.
> >
> > /Peter[/color]
>
> Why is multithreading important to this discussion?[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.