Pre {Line-Height: 1; color: # f0caa6; Background-color: # 2d161d; font-size: 16px ;}. sysfunc {color: # e54ae9; font-style: italic; font-weight: bold ;}. selffuc {color: # f1f9be ;}. bool {color: # 69305e ;}. condition {color: #628698; font-weight: bold ;}. key {color: # e336b6 ;}. vaR {color: #008080; font-style: italic ;}. digit {color: #808080; font-weight: bold ;}. includepre {color: # a6caf0 ;}. operator? {Color: # ce0c40; font-weight: bold ;}
?? Containers in STL are divided into two types by storage method. One is containers stored in arrays (such as: vector? And deque); another type of containers are stored in the form of discontinuous nodes (such as list, set, and map ). When using the erase method to delete elements, you need to pay attention to some problems .?????? In use? List, set? Or? Map traversal can be used to delete some elements as follows:
Use method 1 ?????? STD: List <? Int>? List;
?????? STD: List <? Int >:: iterator? Itlist ;?????? For (? Itlist? =? List. Begin ();? Itlist ?! =? List. End ();?)?????? {???????????? If (? Willdelete (? * Itlist )?)???????????? {??????????????? Itlist? =? List. Erase (? Itlist );????????????}???????????? Else ??????????????? Itlist ++ ;??????}??????? Or
Correct method 2 ?????? STD: List <? Int>? List;
?????? STD: List <? Int >:: iterator? Itlist ;?????? For (? Itlist? =? List. Begin ();? Itlist ?! =? List. End ();?)?????? {???????????? If (? Willdelete (? * Itlist )?)???????????? {??????????????? List. Erase (? Itlist ++ );????????????}???????????? Else ??????????????? Itlist ++ ;??????}???????????? The following are two incorrect usage methods:
Error Method 1 ?????? STD: List <? Int>? List;
?????? STD: List <? Int >:: iterator? Itlist ;?????? For (? Itlist? =? List. Begin ();? Itlist ?! =? List. End ();? Itlist ++ )?????? {???????????? If (? Willdelete (? * Itlist )?)???????????? {??????????????? List. Erase (? Itlist );????????????}??????}????????? Or
Error Method 2 ?????? STD: List <? Int>? List;
?????? STD: List <? Int >:: iterator? Itlist ;?????? For (? Itlist? =? List. Begin ();? Itlist ?! =? List. End ();?)?????? {???????????? If (? Willdelete (? * Itlist )?)???????????? {??????????????? Itlist? =? List. Erase (? + + Itlist );????????????}???????????? Else ??????????????? Itlist ++ ;??????}?????? Correct Method 1: Use the returned value of the erase method to obtain the position of the next element ?????? Correct Method 2: Before calling the erase method? "++" To get the location of the next element ?????? Incorrect Method 1: After the erase method is called, "++" is used to obtain the location of the next element. Because the element location has been deleted after the erase method is called, if you obtain the next location based on the old location, an exception occurs .?????? Error Method 2: Same as above .?????? Here, the "++" operator is the opposite of our normal understanding. Erase (? Itlist ++ )? The location where the next element is obtained is deleted first ;? Erase (? ++ Itlist )? Is to get the location of the next element after deletion .????? In use? When vector and deque are used to traverse and delete elements, you can also obtain the position of the next element through the returned values of erase:
Correct usage ?????? STD: vector <? Int>? VEC;
?????? STD: vector <? Int >:: iterator? Itvec ;?????? For (? Itvec? =? VEC. Begin ();? Itvec ?! =? VEC. End ();?)?????? {???????????? If (? Willdelete (? * Itvec )?)???????????? {????????????????? Itvec? =? VEC. Erase (? Itvec );????????????}???????????? Else ??????????????? Itlist ++ ;??????}???????????? Note: vector, deque? You cannot use method 2 to traverse and delete objects .?
Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.
In STL, the erase () method is used to traverse and delete elements ?. XML