append set to another set

mr.bio picture mr.bio · Apr 9, 2010 · Viewed 44.5k times · Source

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 ?

Answer

CB Bailey picture CB Bailey · Apr 9, 2010

You can insert a range:

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