集合必须是有序的,
merge:将两个有序范围合并到一个新的有序范围中。
set_union:将两个有序集合的并集存储到第三个集合中。
set_difference:将第一个有序集合中不在第二个有序集合中的元素存储到第三个集合中。
set_symmetric_difference:将两个有序集合的对称差(即在其中一个集合中但不在另一个集合中的元素)存储到第三个集合中。
set_intersection:将两个有序集合的交集存储到第三个集合中。
1. merge:将两个有序范围合并到一个新的有序范围中
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> set1 = {1, 3, 5, 7, 9};std::vector<int> set2 = {2, 4, 6, 8, 10};std::vector<int> result;// 预分配结果向量的大小result.resize(set1.size() + set2.size());// 使用 std::merge 合并两个有序集合std::merge(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin());// 输出结果std::cout << "Merged: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:
Merged: 1 2 3 4 5 6 7 8 9 10
2. set_union:将两个有序集合的并集存储到第三个集合中
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), std::back_inserter(result));std::cout << "Union: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:
Union: 1 2 3 4 5 6 7 8
3. set_difference:将第一个有序集合中不在第二个有序集合中的元素存储到第三个集合中
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), std::back_inserter(result));std::cout << "Difference: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:Difference: 1 2 3
如果set1 = {1,2,3,4,4,5}, 则结果是:1 2 3 4
4. set_symmetric_difference:将两个有序集合的对称差(即在其中一个集合中但不在另一个集合中的元素)存储到第三个集合中
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::set_symmetric_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), std::back_inserter(result));std::cout << "Symmetric Difference: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:
Symmetric Difference: 1 2 3 6 7 8
5. set_intersection:将两个有序集合的交集存储到第三个集合中
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), std::back_inserter(result));std::cout << "Intersection: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
output:Intersection: 4 5
使用 C++20 的 ranges 库可以使代码更加简洁和易读。以下是使用 C++20 ranges 实现 std::merge、std::set_union、std::set_difference、std::set_symmetric_difference和 std::set_intersection的示例:
1. std::ranges::merge
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>int main() {std::vector<int> set1 = {1, 3, 5, 7, 9};std::vector<int> set2 = {2, 4, 6, 8, 10};std::vector<int> result;std::ranges::merge(set1, set2, std::back_inserter(result));std::cout << "Merged: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
2. std::ranges::set_union
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::ranges::set_union(set1, set2, std::back_inserter(result));std::cout << "Union: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
3. std::ranges::set_difference
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::ranges::set_difference(set1, set2, std::back_inserter(result));std::cout << "Difference: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
4. std::ranges::set_symmetric_difference
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::ranges::set_symmetric_difference(set1, set2, std::back_inserter(result));std::cout << "Symmetric Difference: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}
5. std::ranges::set_intersection
#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>int main() {std::vector<int> set1 = {1, 2, 3, 4, 5};std::vector<int> set2 = {4, 5, 6, 7, 8};std::vector<int> result;std::ranges::set_intersection(set1, set2, std::back_inserter(result));std::cout << "Intersection: ";for (int n : result) {std::cout << n << " ";}std::cout << std::endl;return 0;
}