Based on the use of erase and remove functions in C++ list

  • 2020-04-02 00:55:51
  • OfStack

erase Is used to invalidate the iterator as a parameter and return the iterator that points to the next parameter of that iterator.
As follows:

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

There is a procedure for erasing an 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);
  }
}

Question: The program cannot break out of the loop
The reason: Test_list. Erase (test_list_it); Each time you do erase, there is a chance that the iterator will fail and an error will occur on test_list_it++. See effective STL. Iterator failure is possible when all containers are doing the erase operation.
To:

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

The 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 wrong because iter_e=test_list_it is a copy of the pointer value, and they both point to the same place, so iter_e fails and so does test_list_it, so test_list_it++ is a problem
If it is
The same code at the page code block index 3
No problem.
The remove function has the same problem as the erase function, but the return value of the remove function is empty, and erase returns an iterator that points to the next element.

Here 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("------------------------------ Startn");
 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------------------------------ Endn");
 getchar();
 return 0;
}

Among them:

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

It can also be written as follows, because the return value of the erase function is an iterator that points 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 results are as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Start
Ls. The empty () = 1
Ls. Max_size () = 1073741823
Ls. The size () = 0
-- after push 1, 2, 3 --
Ls. The empty () = 0
Ls. Max_size () = 1073741823
Ls. The size () = 3
1, 2, 3,
------------------------------
Erase 1
Erase 2
Erase 3
-- -- -- -- -- -- -- -- -- after erase -- -- -- -- -- -- -- -- --
Ls. The empty () = 1
Ls. Max_size () = 1073741823
Ls. The size () = 0
------------------------------
Remove 1
Remove 2
Remove 3
-- -- -- -- -- -- -- -- -- after the remove -- -- -- -- -- -- -- -- --
Ls. The empty () = 1
Ls. Max_size () = 1073741823
Ls. The size () = 0
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the End

Related articles: