From e9b69640afff15e9f16b228ae3df6c1e8507e2fa Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 10 Oct 2023 13:50:58 -0400 Subject: [PATCH] fixing the copy all issue in erase --- lectures/11_list_implementation/list.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lectures/11_list_implementation/list.h b/lectures/11_list_implementation/list.h index 3e120b0..858b680 100644 --- a/lectures/11_list_implementation/list.h +++ b/lectures/11_list_implementation/list.h @@ -144,15 +144,11 @@ class dslist { /* iterator implementation */ void erase(iterator itr){ - dslist::iterator itr2 = itr; - itr2++; - // Question: is this right? - while (itr2 != this->end()) { - *itr = *itr2; - itr++; - itr2++; - } - this->pop_back(); + dslist::iterator itr2(itr.ptr->next); + itr.ptr->prev->next = itr2.ptr; + itr2.ptr->prev = itr.ptr->prev; + delete itr.ptr; + size_ = size_ - 1; } void insert(iterator itr, const T& value){