Set_union with multi-position containers?

What does the std: set_union algorithm return when one or both of the input containers are multisets with duplicate objects? Are duplicates lost?

Suppose, for example:

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() );

What will be the way out?

+5
source share
3 answers

From the standard, 25.3.5:

The semantics of given operations is generalized to multisets in a standard way, determining union()to contain the maximum number of occurrences of each element intersection(), to contain a minimum, etc.

So, in your example, the result will be (1,1,1,2,2,3,4,0,0,0), since you initialized a vector of length 10.

+4
source

std:: set_union ( ).

set_union "union" : , [first1, last1), [first2, last2] . , . , [first1, last1) n [first2, last2) ( m n ), max (m, n) . [1] Set_union , , , , .

max(m,n) , m - , ms1, n - , ms2.

+2

++, §25.3.5/1:

. (23.3.4), . , union(), , (), , ..

+2

All Articles