c++ - set_union 与 multiset 容器?

标签 c++ stl containers stl-algorithm

当一个或两个输入容器是具有重复对象的多重集时,算法 std:set_union 的返回值是多少? dups 会迷路吗?

让我们假设例如:

multiset<int> ms1;
ms1.insert(1);
ms1.insert(1);
ms1.insert(1);
ms1.insert(2);
ms1.insert(3);

multiset<int> ms2;
ms2.insert(1);
ms2.insert(1);
ms2.insert(2);
ms2.insert(2);
ms2.insert(4);

vector<int> v(10);
set_union( ms1.begin(), ms1.end(), ms2.begin(), ms2.end(), v.begin() );

输出是什么?

最佳答案

根据标准 25.3.5:

The semantics of the set operations are generalised to multisets in a standard way by defining union() to contain the maximum number of occurrences of every element, intersection() to contain the minimum, and so on.

因此在您的示例中,结果将为 (1,1,1,2,2,3,4,0,0,0),因为您初始化了长度为 10 的 vector 。

关于c++ - set_union 与 multiset 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3195973/

相关文章:

c++ - STL 算法将整个容器而不是 .begin(), end() 作为 arg?

docker - 关闭 Docker 容器耗时过长

模板类上的 C++ 运算符重载

c++ - 合并多个 STL 容器并删除重复元素的最佳方法?

C++ 对字符串中的字符进行反向排序

c++ - 用于插入的指针包装器

指针存储在容器中的对象的 C++ 分配器

c++ - 在现代OpenGL中,使用GL_TRIANGLE奇怪的z轴行为绘制实心圆

c++ - 循环读取套接字回复

c++ - 计算子字符串的出现次数