From facdca29702e653ddfa74027538369de5e31c2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 29 Nov 2014 16:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90merge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/List.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) 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