Insert () and erase () of STL list ()

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.