Usage of erase and remove functions in C ++ list

Source: Internet
Author: User

The role of erase is to invalidate the iterator as a parameter and return the iterator pointing to the next parameter of the iterator.

As follows:

list ParticleSystem;list::iterator pointer;if(pointer->dead == true){   pointer = ParticleSystem.erase(pointer);}

There is a program about erase error.

using namespace std;int main(){  std::listtest_list;  std::list::iterator test_list_it;  test_list.push_back(1);  test_list_it = test_list.begin();  for(;test_list_it != test_list.end();test_list_it++)  {  test_list.erase(test_list_it);  }}

Problem: The program cannot jump out of the loop

Cause: test_list.erase (test_list_it); The iterator may be invalidated every time the erase is executed. test_list_it ++ will cause an error. For more information, see objective STL. All containers may invalidate the iterator when performing erase operations.

Changed:

for(;test_list_it != test_list.end();){    test_list.erase(test_list_it++);}

Or

for(;test_list_it != test_list.end();){    std::list::iterator iter_e=test_list_it++;    test_list.erase(iter_e);}

Note:

for(;test_list_it != test_list.end();test_list_it++;) {    std::list::iterator iter_e=test_list_it;    test_list.erase(iter_e);}

This is still an error because iter_e = test_list_it is a copy of the pointer value, which actually points to the same location, so if iter_e is invalid, test_list_it will also become invalid, so test_list_it ++ has a problem.
If yes

for(;test_list_it != test_list.end();){    std::list::iterator iter_e=test_list_it++;    test_list.erase(iter_e);}

Then there is no problem.

From above: http://blog.sina.com.cn/s/blog_782496390100rtyp.html

The remove function also has the same erase function problem, but the return value of the remove function is null. Erase returns the iterator pointing to the next element.

 

The following is a simple example.

#include "stdafx.h"#include <stdio.h>#include <string.h>#include <malloc.h>#include <list>using namespace std;int _tmain(int argc, _TCHAR* argv[]){printf("------------------------------ Start\n");list<int> ls;printf("ls.empty() = %d \n", ls.empty());printf("ls.max_size() = %d \n", ls.max_size());printf("ls.size() = %d \n", ls.size());ls.push_back(1);ls.push_back(2);ls.push_back(3);printf("\n--------- after push 1, 2, 3 ---------\n");printf("ls.empty() = %d \n", ls.empty());printf("ls.max_size() = %d \n", ls.max_size());printf("ls.size() = %d \n", ls.size());for (list<int>::iterator i = ls.begin(); i != ls.end(); i++) {printf("%d, ", *i);}printf("\n------------------------------\n");for (list<int>::iterator i = ls.begin(); i != ls.end(); ) {printf("erase %d \n", *i);ls.erase(i++);}printf("\n--------- after erase ---------\n");printf("ls.empty() = %d \n", ls.empty());printf("ls.max_size() = %d \n", ls.max_size());printf("ls.size() = %d \n", ls.size());printf("\n------------------------------\n");ls.push_back(1);ls.push_back(2);ls.push_back(3);for (list<int>::iterator i = ls.begin(); i != ls.end(); ) {printf("remove %d \n", *i);ls.remove(*i++);}printf("\n--------- after remove ---------\n");printf("ls.empty() = %d \n", ls.empty());printf("ls.max_size() = %d \n", ls.max_size());printf("ls.size() = %d \n", ls.size());printf("\n------------------------------ End\n");getchar();return 0;}

Where:

for (list<int>::iterator i = ls.begin(); i != ls.end(); ) {printf("erase %d \n", *i);ls.erase(i++);}

You can also write it as follows, because the returned value of the erase function is the iterator pointing to the next element.

for (list<int>::iterator i = ls.begin(); i != ls.end(); ) {printf("erase %d \n", *i);i = ls.erase(i);}

The output result is as follows:

------------------------------ Start
Ls. Empty () = 1
Ls. max_size () = 1073741823
Ls. Size () = 0

--------- After push 1, 2, 3 ---------
Ls. Empty () = 0
Ls. max_size () = 1073741823
Ls. Size () = 3
1, 2, 3,
------------------------------
Erase 1
Erase 2
Erase 3

--------- After erase ---------
Ls. Empty () = 1
Ls. max_size () = 1073741823
Ls. Size () = 0

------------------------------
Remove 1
Remove 2
Remove 3

--------- After remove ---------
Ls. Empty () = 1
Ls. max_size () = 1073741823
Ls. Size () = 0

------------------------------ End

 

 

Related Article

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.