Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set_union with multiset containers?

What's the return of the algorithm std:set_union when one or both input containers are multisets with duplicated objects? Do dups get lost?

Let's 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 would the output be?

like image 416
Gianluca Avatar asked Jul 07 '10 14:07

Gianluca


1 Answers

From the standard, 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.

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

like image 186
Mike Seymour Avatar answered Oct 28 '22 22:10

Mike Seymour