How to Prevent the use of erase () when the iterator fails, such as map, and other associated containers, such as vector?

Source: Internet
Author: User

Sequent container: (vector)

Erase not only invalidates all iterators pointing to the deleted element, but also invalidates all iterators after the deleted element. Therefore, the erase (ITER ++) method cannot be used, however, the returned value of erase is the next effective iterator. The correct method is ::For (iter = C. Begin (); iter! = C. End ();)

Iter = C. Erase (ITER );

Associated container: (MAP)The erase iterator is invalid only for the iterator whose elements are deleted, but the returned value is void. Therefore, erase (ITER ++) is used to delete the iterator, The correct method is ::

For (iter = C. Begin (); iter! = C. End ();)

C. Erase (ITER ++ );

========================================================== ==================================

STD: map is a very common standard container. It uses a red/black tree or a balanced binary tree to store node content. It has a log-complex insertion time and search time. Here are some of its important points of attention.

Insert:STD: Map <int, STD: String> str_map; str_map.insert (STD: pair <const int, STD: String> (2, "BB ")); // There is no Transformation Operation str_map.insert (STD: pair <int, STD: String> (2, "BB"); // it needs to be transformed into STD :: pair <const int, STD: String> and then insert str_map.insert (STD: make_pair (3, "cc"); // same as above, you need to transform str_map.insert (STD :: map <int, STD: String >:: value_type (4, "DD"); // if there is no transformation operation, you can directly insert it through the indexer, this method will be discussed below. Delete:A common error is: For (Map <int, string >:: iterator it = str_map.begin (); it! = Str_map.end (); It ++) {If (some_condition) str_map.erase (IT) ;}the delete operation will make it messy, and then it ++ will cause an error. The correct method is: For (Map <int, string >:: iterator it = str_map.begin (); it! = Str_map.end ();) {If (some_condition) {str_map.erase (it ++) ;}else {It ++ ;}} Index:Str_map [5] = "ee"; this statement is actually executed in two steps: Create an empty string in the place of str_map [5] First, then return the reference of this string through str_map [5], call the assignment operator of this empty string, and assign "ee" to it. Therefore, writing is less efficient than directly inserting data. There is another issue with indexes: Map <int, string> m; cout <m. size () <Endl; // output: 0if (M [4] = "AA") some_operation (); cout <m. size () <Endl; // output: 1 here m [4] has pointed to a constructed empty string Function object:With the help of STD: mem_fun, containers such as vector can easily use generic algorithms such as find_if, such as class X {public: bool condition ();}; vector <x> VX ;.... vector <X >:: iterator it = STD: find_if (VX. begin (), VX. end (), STD: mem_fun (& X: condition); Because map: iterator points to STD: pair, therefore, to use these algorithms, You have to manually write the corresponding function object. However, with the help of the boost Lambda library, we can save the trouble of writing function objects by ourselves: # include <boost/lambda/bind. HPP> # include <boost/lambda/Lambda. HPP> using boost: Lambda: bind; STD: Map <int, x> MX ;.... STD: Map <int, X >:: iterator it = find_if (MX. begin (), MX. end (), BIND (& X: condition, BIND (& STD: Map <int, X >:: value_type: Second, _ 1); tips:

In fact, both methods of list can work normally.

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.