Nic*_*tti 114
你可以这样做:
std::set<int> s1;
std::set<int> s2;
// fill your sets
s1.insert(s2.begin(), s2.end());
Run Code Online (Sandbox Code Playgroud)
Ant*_*rez 36
看起来像你要求的std::set_union.
例:
#include <set>
#include <algorithm>
std::set<int> s1;
std::set<int> s2;
std::set<int> s3;
// Fill s1 and s2
std::set_union(std::begin(s1), std::end(s1),
std::begin(s2), std::end(s2),
std::inserter(s3, std::begin(s3)));
// s3 now contains the union of s1 and s2
Run Code Online (Sandbox Code Playgroud)
Man*_*ddy 10
使用C++ 17,您可以直接使用merge功能set.
当您希望将set2元素作为合并的一部分提取并插入set1时,这样会更好.
如下所示:
set<int> set1{ 1, 2, 3 };
set<int> set2{ 1, 4, 5 };
// set1 has 1 2 3 set2 has 1 4 5
set1.merge(set2);
// set1 now has 1 2 3 4 5 set2 now has 1 (duplicates are left in the source, set2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57230 次 |
| 最近记录: |