From 475d32e940630cbb123d6558adb11e5b040d0351 Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Fri, 10 Jul 2020 11:38:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=20=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=88=A0=E9=99=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- .../Cpp_Standard_Library/vector_补充说明.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Software/Development/Library/Cpp_Standard_Library/vector_补充说明.md b/Software/Development/Library/Cpp_Standard_Library/vector_补充说明.md index c39e84e..966d2bd 100644 --- a/Software/Development/Library/Cpp_Standard_Library/vector_补充说明.md +++ b/Software/Development/Library/Cpp_Standard_Library/vector_补充说明.md @@ -24,3 +24,20 @@ for(itr=list.begin(); itr!=list.end();) ``` 这里,end() 指向最后一个元素的下一个位置;调用 delete (*itr) 释放对象;调用 erase() 后,itr 会自动 +1。因此 delete 必须在 erase() 前调用,由于 delete 后,只是析构了对象而没有修改 list 中的成员,因此可以继续调用 list.erase(itr),释放 list 中对应的成员。 + +如果是有条件的删除,则: + +```cpp +vector::iterator itr; + +for(itr=list.begin(); itr!=list.end();) +{ + if(...) + { + delete (*itr); + list.erase(itr); + } + else + itr++; +} +```