Tips for deleting C ++ container values

Source: Internet
Author: User

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. So let's look at the code below and repost it 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 difference between ++ I and I ++.) Well, 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.