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: 227

Filed under:
|
|
|

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;
}
  1. Is it better to use

    std::swap( merged_options, old_options );
    

    or the assignment I have?

  2. Is there a better way to filter duplicates and return the merged set than consecutive calls to std::set_intersection and std::set_union to detect dupes and merge the sets? 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

Related posts about c++

Related posts about algorithm