C + + std::map::erase usage and its pitfalls

Source: Internet
Author: User

1. Introduction of:

The STL map has a erase method to remove the established node from a map

eg

map<string,string> maptest;  typedef map<string,string>:: Iterator ITER;  ITER iter=maptest.find (key);  Maptest.erase (ITER);

Like the above delete a single node, map behavior will not be problematic, but when used in a loop, it is often misused.

2. Traps

eg

 for (Iter Iter=maptest.begin (); Iter!=maptest.end (); +iter)  {  cout<<iter->first<<":"<<iter->second<<  Endl;  Maptest.erase (ITER);  }  

This is a wrong way of writing, cause the program behavior is unknown, because map is an associative container, for the associated container, if an element has been deleted, then its corresponding iterator is invalidated, should not be used, responsible for the program will cause undefined behavior.

3. Correct usage

Correct usage One: (c++11 can, c++98 not)

The erase () member function returns an iterator to the next element

STD::MAP<STD::string, std::string >::iterator it = maptest.begin ();    while (It! = maptest.end ())  {           if(TestVal (it->second))           {                   = maptest.erase (it);           }            Else                    It++  ;  }

Correct usage Two:

Locates the next element using the iterator before the delete. How STL recommendations are used

 for // Note that you can't write iter++  here anymore . {  cout<<iter->first<<":"<<iter->second<< Endl;  Maptest.erase (ITER+ +)  ;  }
In this usage, the method takes advantage of the Post + + feature, which executes Maptest.erase (it++) at this time; This statement is divided into three processes

1, first assign the value of it to a temporary variable as a parameter variable passed to erase

2. Because the parameter processing takes precedence over the function call, then the it++ operation is performed, that is, it now points to the next address.

3. Call the Erase function again to release the location of the temporary variable that was saved in the first step for the value of it to be deleted.

4. Example

CASE1:?

#include <map>#include<iostream>intMain () {Std::map<int,int>map_a; Std::map<int,int>:: Iterator it;  for(inti =0; I! =Ten; i++) {Map_a.insert (Std::map<int,int>:: Value_type (I, i)); }         for(it = Map_a.begin (); It! = Map_a.end (); it++) {Std::cout<< It->first <<Std::endl; //map_a.erase (it->first);        }        return 0;}

Results:

0 1 2 3 4 5 6 7 8 9

CASE2:?

#include <map>#include<iostream>intMain () {Std::map<int,int>map_a; Std::map<int,int>:: Iterator it;  for(inti =0; I! =Ten; i++) {Map_a.insert (Std::map<int,int>:: Value_type (I, i)); }         for(it = Map_a.begin (); It! = Map_a.end (); it++) {Std::cout<< It->first <<Std::endl; Map_a.erase (It-First ); }        return 0;}

Results:

0 3

When the map size is added to 1000000, the core

5. Causes of unknown program behavior

The erase method of the 5.1 list and vector returns a iterator that points to the next element of the deleted element, except that the 4 of the Map,set,multimap,multiset return void.

But in the c++11 has been corrected, all returned to a iterator

  

Cause: The operation of the iterator + + is to manipulate the node pointer it holds, so when the node is deleted, the pointer to the node is not deterministic, that is, it may be the previous data, or it may have other irrelevant data, such behavior is called undefined behavior, not necessarily the core.

C + + std::map::erase usage and its pitfalls

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.