append set to another set

Is there a better way of appending a set to another set than iterating through each element ?

i have :

set<string> foo ;
set<string> bar ;

.....

for (set<string>::const_iterator p = foo.begin( );p != foo.end( ); ++p)
    bar.insert(*p);

Is there a more efficient way to do this ?

Answers


You can insert a range:

bar.insert(foo.begin(), foo.end());

It is not a more efficient but less code.

bar.insert(foo.begin(), foo.end());

Or take the union which deals efficiently with duplicates. (if applicable)

set<string> baz ;

set_union(foo.begin(), foo.end(),
      bar.begin(), bar.end(),
      inserter(baz, baz.begin()));

Need Your Help

manage.py help has different python path in virtualenv

python django virtualenv pythonpath manage.py

I have a problem in virtualenv that a wrong python path is imported.

Ambiguous reference to member 'count'

ios arrays xcode swift swift2

Getting the following error when i migrated my current Xcode (v 7.0.1) project to Xcode 7.1.1