Tips for deleting C ++ container values

Source: Internet
Author: User

There are still many implementation methods for deleting C ++ container values. What we will introduce today is one of the commonly used and simple implementation methods, hoping to help you.

In the process of program development, containers in the C ++ programming language have many values, some of which are useful and some are useless. So how should we delete these useless values? Here we will introduce in detail how to delete the C ++ container value.

Generally, the erase function is provided in the C ++ container. One of the parameters received by this function is generally an iterator:

If you delete the C ++ container value, we may have used it in general:

 
 
  1. List <int> C;
  2. // Todo insert items
  3. For (list <int >:: iterator I = C. Begin (); I! = C. End (); ++ I)
  4. {
  5. If (* I)> 10)
  6. {
  7. // If there is a value greater than 10, delete it
  8. C. Erase (I );
  9. Break;
  10. }
  11. }

The above code is correct when deleting an element... However, we want to delete all values greater than 10, so:

 
 
  1. List <int> C;
  2. // Todo insert items
  3. For (list <int >:: iterator I = C. Begin (); I! = C. End (); ++ I)
  4. {
  5. If (* I)> 10)
  6. {
  7. // Delete all values greater than 10
  8. C. Erase (I );
  9. }
  10. }

Compilation and running with hope... So an exception occurs... Ah... Oh...

It turns out that after the iterator I is deleted, the element I refers to has expired, and then I ++ is given. It no longer exists...The code for deleting the C ++ container value is as follows:

 
 
  1. List <int> C;
  2. // Todo insert items
  3. List <int>: iterator nextitr = C. Begin ();
  4. For (list <int >:: iterator I = C. Begin ();;)
  5. {
  6. If (nextitr = C. End ())
  7. Break;
  8. ++ Nextitr;
  9. If (* I)> 10)
  10. {
  11. // If there is a value greater than 10, delete it
  12. C. Erase (I );
  13. }
  14. I = nextitr;
  15. }

The above code is easy to understand, that is, before deleting an iterator, store its later iterator first, and then use the previously stored iterator in the next loop.

OK, we can see that the above Code can work, and the behavior seems to be correct,... The Code seems to be a little more. I think it is better to reduce the number of codes, and the logic should not be so troublesome. Then let's look at the following code (Reprinted from objective STL ).

 
 
  1. List <int> C;
  2. // Todo insert items
  3. For (list <int >:: iterator I = C. Begin (); I! = C. End ();)
  4. {
  5. If (* I)> 10)
  6. {
  7. // If there is a value greater than 10, delete it
  8. C. Erase (I ++ );
  9. }
  10. Else
  11. I ++;
  12. }

Well... A master is a master (I have never cared about the big difference between ++ I and I ++ in the past). I will provide another version, use the remove_if function of list.

 
 
  1. bool fun(int i)  
  2. {  
  3. if(i>10)  
  4. return true;  
  5. else  
  6. return false;  
  7. }  
  8. list<int> c;  
  9. // todo insert items  
  10. c.remove_if(fun); 

Well, there are many ways to delete C ++ container values.

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.