Index: list.hpp =================================================================== --- list.hpp (revision 51474) +++ list.hpp (working copy) @@ -1021,21 +1023,36 @@ template void merge(list_impl& x, Predicate p) { - const_iterator e(this->end()); - const_iterator bx(x.begin()); - const_iterator ex(x.end()); + const_iterator b = this->cbegin(), e = this->cend(); + const_iterator ex = x.cend(); - for (const_iterator b = this->cbegin(); b != e; ++b) { - size_type n(0); - const_iterator ix(bx); - while(ix != ex && p(*ix, *b)){ - ++ix; ++n; + while (!x.empty()) + { + const_iterator bx = x.cbegin(); + while (b != e && p(*bx, *b)) + { + ++b; } - this->splice(b, x, bx, ix, n); - bx = ix; + + if (b != e) + { + // determine the end of the range of x to inject at b + size_type n = 0; + do + { + ++bx; + ++n; + } + while (bx != ex && p(*bx, *b)); + this->splice(b, x, x.cbegin(), bx, n); + } + else + { + // the rest of the list is to be appended to the end + this->splice(e, x); + break; + } } - //Now transfer the rest at the end of the container - this->splice(e, x); } //! Effects: Reverses the order of elements in the list.