Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of deleting C++ container value?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the methods related to deleting the value of C++. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

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

The erase function is generally provided in C++ containers, and one of the parameters received by this function is an iterator:

If we delete the C++ capacity value, we may have generally used it:

List c; / / todo insert items for (list::iterator I = c.begin (); I = c.end (); + + I) {if ((* I) > 10) {/ / if there is a value greater than 10, deleted c.erase (I); break;}}

The above code has no problem when deleting an element. But we want to delete all values greater than 10, so:

List c; / / todo insert items for (list::iterator I = c.begin (); I = c.end (); + + I) {if ((* I) > 10) {/ / remove all values greater than 10 c.erase (I);}}

Compile and run hopefully. So the anomaly happened. no, no, no. no, no, no. Ah. Oh. No, no, no.

It turns out that after deleting the iterator I, the element I refers to has been invalidated, and then given to the iiterator, it no longer exists. So I racked my brains and came up with the following value code for deleting C++ containers:

List c; / / todo insert items list::iterator nextitr = c.begin (); for (list::iterator I = c.begin ();) {if (nextitr = = c.end ()) break; + + nextitr; if ((* I) > 10) {/ / if there is a value greater than 10, deleted c.erase (I);} I = nextitr;}

The above code is easy to understand, that is, before deleting an iterator, store its subsequent iterator and then take advantage of the previously stored iterator on the next loop.

OK, we see that the above code works, and the behavior seems to be correct, but. There seems to be too much code. I wish there was less code and less trouble with logic. So let's look at the following code (reproduced from Effective STL).

List c; / / todo insert items for (list::iterator I = c.begin (); iTunes = c.end ();) {if ((* I) > 10) {/ / if there is a value greater than 10, the deleted c.erase (iTunes +);} else iTunes;}

Mm-hmm. no, no, no. A master is a master (I never cared about the difference between + + I and iTunes +). OK, * provide another version, using the remove_if function of list.

Bool fun (int I) {if (I > 10) return true; else return false;} list c; / / todo insert items c.remove_if (fun)

Well, there are actually a lot of ways to delete C++ capacity values.

The above is the editor for you to share the deletion of C++ storage value-related method is how, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report