The List class provides the insert () and erase () functions, which add and delete an element at the iterator position respectively.
1. insert ()
Iterator insert (iterator POs, const T & vaule); // insert vaule before the POs to return the iterator of the new element without affecting the existing iterator.
# Include <iostream> using namespace STD; # include <list> int main (void) {int A [5] = {1, 2, 3, 4, 5 }; list <int> intlist (A, A + 5); List <int>: iterator ITER, newiter; iter = intlist. begin (); newiter = intlist. insert (ITER, 0); cout <"newiter points to:" <* newiter <Endl; cout <"ITER points to:" <* ITER <Endl; getchar ();}
Running result:
Insert data element 0 to the first element of the table. newiter points to the newly inserted 0, and ITER points to the original 1.
2, erase ()
Void erase (iterator POS); // Delete the elements pointed to by the POs. After deletion, the POs points to unknown.
# Include <iostream> using namespace STD; # include <list> int main (void) {int A [5] = {1, 2, 3, 4, 5 }; list <int> intlist (A, A + 5); List <int>: iterator ITER, newiter; iter = intlist. begin (); intlist. erase (ITER); cout <"ITER points to:" <* ITER <Endl; // The runtime error getchar ();}
Running result:
Delete the first element of the table. The table has one element missing, But ITER points to unknown. This is not what we expected!
Therefore, we can use
Erase (ITER ++) replaces erase (ITER)
Erase (ITER ++) is equivalent to giving the current iterator value to the erase parameter, and pointing the iterator ITER to the next element.
# Include <iostream> using namespace STD; # include <list> int main (void) {int A [5] = {1, 2, 3, 4, 5 }; list <int> intlist (A, A + 5); List <int>: iterator ITER, newiter; iter = intlist. begin (); intlist. erase (ITER ++); cout <"ITER points to:" <* ITER <Endl; // The runtime error getchar () is reported ();}
Running result:
3. Use Insert () and erase ()
/* Original array: 1 2 3 4 5 expected output: 1 2 2 3 3 4 4 5 expected output: 3 4 5 5 function doubledata () Use Insert () implement the data element repetition function erasesmallvaule (). Use erase () to delete a data element whose size is smaller than 3. Print () traverses the list, output list information */# include <iostream> using namespace STD; # include <list> template <typename T> void print (list <t> & alist ); template <typename T> void doubledata (list <t> & alist); Template <typename T> void erasesmallvaule (list <t> & alist, t vaule); int main (void) {int A [5] = {1, 2, 3, 4, 5}; List <int> intlist (A, A + 5); cout <"original list is:"; print (intlist ); doubledata (intlist); after cout <"doubledata (), the list is:"; print (intlist); after cout <"deleting elements smaller than 3, the list is :"; erasesmallvaule (intlist, 3); print (intlist); getchar ();} template <typename T> void print (list <t> & alist) {list <t> :: iterator ITER; iter = alist. begin (); While (ITER! = Alist. end () {cout <* ITER <""; ITER ++;} cout <Endl ;} template <typename T> void doubledata (list <t> & alist) {list <t>: iterator ITER, newiter; iter = alist. begin (); While (ITER! = Alist. end () {newiter = alist. insert (ITER, * ITER); ITER ++ ;}} template <typename T> void erasesmallvaule (list <t> & alist, t vaule) {list <t> :: iterator ITER; iter = alist. begin (); While (ITER! = Alist. End () {If (* ITER <vaule) {alist. Erase (ITER ++);} else ITER ++ ;}}
Running result: