Fast swapping of STL containers

J

Jorgen Grahn

std::foo<bar> *a = ...;
std::foo<bar> *b = ...;

You don't have to swap the contents, you can swap pointers to the
containers:

std::swap(a, b); // O(1)

Regarding swapping actual containers or references to containers, you
might get lucky and the swap is optimized for the containers,

"Might get lucky" is too pessimistic. All standard containers I know of
have a fast, non-throwing swap. "ittium" should read the
documentation for whatever container he or she is using.

/Jorgen
 
H

hanukas

"Might get lucky" is too pessimistic. All standard containers I know of
have a fast, non-throwing swap.  "ittium" should read the
documentation for whatever container he or she is using.

Probably don't have to go there in that case; it won't turn up as
performance bottleneck in that case in most likely twist of events. I
was commenting for scenario where it would/might be as the poster
seemed overly concerned. ;)
 

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
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top