The use of erase and remove functions in C + + list _c language

Source: Internet
Author: User
The function of the erase is to invalidate the iterator as a parameter and return the iterator that points to the next parameter of the iterator.
As follows:
Copy Code code as follows:

List Particlesystem;
List::iterator pointer;
if (Pointer->dead = = True)
{
pointer = particlesystem.erase (pointer);
}

There is a program about using erase incorrectly.
Copy Code code as follows:

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 jump out of the loop
Reason:Test_list.erase (test_list_it); Every time you do erase it is possible to invalidate the iterator, and test_list_it++ error occurs. You can see the effective STL book. All containers may fail the iterator when doing erase operations.
replaced by:
Copy Code code as follows:

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

Or
Copy Code code as follows:

for (; Test_list_it!= test_list.end ();)
{
Std::list::iterator iter_e=test_list_it++;
Test_list.erase (Iter_e);
}

Note:
Copy Code code as follows:

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 the copy of the pointer value, they actually point to the same position, so the iter_e failure so test_list_it will be ineffective, so test_list_it++ will have problems
If it is
Copy Code code as follows:

for (; Test_list_it!= test_list.end ();)
{
Std::list::iterator iter_e=test_list_it++;
Test_list.erase (Iter_e);
}

is no problem.
The Remove function also has the same problem with the erase function, but the Remove function return value is NULL, ERASE returns the iterator that points to the next element.

The following is a simple example.
Copy Code code as follows:

#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 ("\---------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 ("\---------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;
}

which
Copy Code code as follows:

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

It can also be written in the following form, because the return value of the erase function refers to the iterator of the next element.
Copy Code code as follows:

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.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.