Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::merge and equal element order

Tags:

c++

c++11

std::merge preserves order of equal elements in its input lists. Does it guarantee that elements from the first list come before equal elements from the second list, or does that guarantee only apply to equal elements within a single input list?

Example:

List1 has 1 element, A. List2 has 1 element, B. Comparator considers A and B to be equal.

If I std::merge(list1.begin(), list1.end(), list2.begin(), list2.end(), out, comparator), is the relative order of A and B in the output defined?

My opinion is the standard does not define order in this case.

like image 469
John F. Carr Avatar asked Dec 17 '15 20:12

John F. Carr


1 Answers

C++14 standard draft (n3797):

17.6.5.7/1

When the requirements for an algorithm state that it is “stable” without further elaboration, it means:
— For the merge algorithms, for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).

like image 61
Revolver_Ocelot Avatar answered Oct 22 '22 15:10

Revolver_Ocelot