diff --git a/TinySTL/List.h b/TinySTL/List.h index bd7b3ad..bb217d7 100644 --- a/TinySTL/List.h +++ b/TinySTL/List.h @@ -143,9 +143,9 @@ namespace TinySTL{ void unique(); template void unique(BinaryPredicate binary_pred); - //void merge(list& x); - //template - //void merge(list& x, Compare comp); + void merge(list& x); + template + void merge(list& x, Compare comp); //void sort(); //template //void sort(Compare comp); @@ -370,6 +370,37 @@ namespace TinySTL{ auto next = i; this->splice(position, x, i, ++next); } + template + void list::merge(list& x){ + auto it1 = begin(), it2 = x.begin(); + while (it1 != end() && it2 != x.end()){ + if (*it1 <= *it2) + ++it1; + else{ + auto temp = it2++; + this->splice(it1, x, temp); + } + } + if (it1 == end()){ + this->splice(it1, x, it2, x.end()); + } + } + template + template + void list::merge(list& x, Compare comp){ + auto it1 = begin(), it2 = x.begin(); + while (it1 != end() && it2 != x.end()){ + if (comp(*it2, *it1)){ + auto temp = it2++; + this->splice(it1, x, temp); + } + else + ++it1; + } + if (it1 == end()){ + this->splice(it1, x, it2, x.end()); + } + } } #endif \ No newline at end of file