完成merge

This commit is contained in:
邹晓航
2014-11-29 16:01:09 +08:00
parent f41ffea657
commit facdca2970

View File

@@ -143,9 +143,9 @@ namespace TinySTL{
void unique(); void unique();
template <class BinaryPredicate> template <class BinaryPredicate>
void unique(BinaryPredicate binary_pred); void unique(BinaryPredicate binary_pred);
//void merge(list& x); void merge(list& x);
//template <class Compare> template <class Compare>
//void merge(list& x, Compare comp); void merge(list& x, Compare comp);
//void sort(); //void sort();
//template <class Compare> //template <class Compare>
//void sort(Compare comp); //void sort(Compare comp);
@@ -370,6 +370,37 @@ namespace TinySTL{
auto next = i; auto next = i;
this->splice(position, x, i, ++next); this->splice(position, x, i, ++next);
} }
template<class T>
void list<T>::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<class T>
template <class Compare>
void list<T>::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 #endif