[Cs3] Sequential Containers PowerPoint Slides

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Oct 2 10:17:26 EDT 2017


> 
> I have a question / possible correction,  regarding the powerpoint slides
> for STL Sequential Containers.
> 
> On slide 23:
> merge() ??? merges two sorted lists
> 
> 
> I'm not sure if this is implying that the two lists being merged must be
> sorted.
> I tested out merge on two unordered lists and while the result was a bit
> scrambled (all values were there, but in a different than original order),
> It did merge the two lists together.
> 
> I am also aware of what the c++ standard says:
> This function requires that the list <http://www.cplusplus.com/list> containers
> have their elements already ordered by value
> 
> Regardless of this, it still seems to work under clang++.
> 
> Is there anything else I should be aware of about merge(), or perhaps
> there's something I am missing regarding it's operation?
> 

The operation of merge() is correct when the two lists are sorted. The
behavior is unpredictable otherwise. This unpredictability includes a
scrambled moving of the two lists. 

The list merge implementation algorithm is rather straightforward:
maintain an iterator at each source list, move the smaller element to
the target list and advance the respective iterator. Note that this
algorithm fails if the source lists are not sorted.

In general, it is considered poor style to rely on behavior that is
not specfied by the standard. 

Thanks,
--
Mikhail


More information about the cs3 mailing list