Boost does not give a merge method to merge set or multiset.
So I need to do something like this to merge.
int main() {
boost::container::flat_multiset<int> set1 = {1, 2, 3};
boost::container::flat_multiset<int> set2 = {3, 4, 5};
// Manually insert elements from set2 into set1
for (const auto& value : set2) {
set1.insert(value);
}
}
Is it efficient? How to deal with multiple sets ?
So, I went ahead and checked my prediction:
Indeed, merging 100 random flat multisets of 10'000 elements is MUCH faster using the specialized order insertion than the naive approach. Doing it correctly with
std::mergeis very clumsy because you cannot insert directly into the destination anyways:So no, you most definitely don't want
std::merge.You CAN do a lot of work and manually inplace-merge into a fixed sequence to then adopt into a multiset, but that's not worth the effort, and merely does the same as the ordered insertion anyways. This becomes even more felt if you need to support unique as well as multi-sets, or sets with custom order predicates.
Live On Coliru
Printing
Or locally: