开发者

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<str开发者_开发问答ing> 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 ?


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()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜