assignment vs std::swap and merging and keeping duplicates in seperate object
Posted
by
rubenvb
on Stack Overflow
See other posts from Stack Overflow
or by rubenvb
Published on 2011-03-06T16:07:24Z
Indexed on
2011/03/06
16:10 UTC
Read the original article
Hit count: 285
Say I have two std::set<std::string>s. The first one, old_options, needs to be merged with additional options, contained in new_options. I can't just use std::merge (well, I do, but not only that) because I also check for doubles and warn the user about this accordingly. To this effect, I have
void merge_options( set<string> &old_options, const set<string> &new_options )
{
// find duplicates and create merged_options, a stringset containing the merged options
// handle duplicated the way I want to
// ...
old_options = merged_options;
}
Is it better to use
std::swap( merged_options, old_options );or the assignment I have?
Is there a better way to filter duplicates and return the merged
setthan consecutive calls tostd::set_intersectionandstd::set_unionto detect dupes and merge thesets? I know it's slower than one traversal and doing both at once, but these sets are small (performance is not critical) and I trust the Standard more than I trust myself.
© Stack Overflow or respective owner