append set to another set

前端 未结 2 588
轮回少年
轮回少年 2020-12-28 11:17

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

i have :

set foo ;
set bar          


        
2条回答
  •  时光说笑
    2020-12-28 12:02

    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 baz ;
    
    set_union(foo.begin(), foo.end(),
          bar.begin(), bar.end(),
          inserter(baz, baz.begin()));
    

提交回复
热议问题