append set to another set

前端 未结 2 589
轮回少年
轮回少年 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<string> baz ;
    
    set_union(foo.begin(), foo.end(),
          bar.begin(), bar.end(),
          inserter(baz, baz.begin()));
    
    0 讨论(0)
  • 2020-12-28 12:08

    You can insert a range:

    bar.insert(foo.begin(), foo.end());
    
    0 讨论(0)
提交回复
热议问题