C++.stl Map::erase Traps

Source: Internet
Author: User

The difference of map::erase function in different versions of STL 1. c++98 and C++11 standards

http://www.cplusplus.com/reference/map/map/erase/

PJ STL (Windows)

       map::erase function (C++11 standard) returns a map::iterator:

iterator  Map::erase(const_iterator _where); iterator  Map::erase(const_iteratorconst_iterator _last); size_type Map::erase(constkey_type& _keyval);
3. SGI STL (Linux)

The Linux implementation version of the Map::erase function (c++98 standard) returns a void:

void       Map::erase(iterator __position); void       Map::erase(iteratoriterator __last);  Size_type Map::erase(constkey_type& __x);
How to use the Map::erase function correctly

How to use the Map::erase function correctly:

typedef STD::Map<int,int> Kg_testmap; typedef std::map <int,int>::iterator kg_testmapiter; std::map <int,int>maptest; for(Kg_testmapiteriter = Maptest.begin(); Iter! =Maptest. End ();) {maptest. Erase (ITER++);}

This approach leverages the features of the Post + +, and executes the maptest.erase (iter++) statement into two processes:
• The Post + + operation will save a copy of TMP for the current ITER, then increment the current ITER to point to the next element, and finally return the copy TMP

• Call the Map::erase function, the element that TMP points to.

//code in PJ STL_myiter operator++(int){ //postincrement    _myiter_tmp = * This; ++* This; return(_tmp);}

Code in SGI STL
_self operator+ + (int)
{
_self __tmp = *this;
_m_increment ();
return __tmp;
}

C++.stl Map::erase Traps

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.