Find,erase method of correctly using STL map

Source: Internet
Author: User

There are 2 ways to find the value corresponding to a key:
Method 1:

typedef std::multimap<boost::uint64_t,std::string>::iterator IT
it = Mmap_.find (Ncallletter);
if (It! = Mmap_.end ())
{
Char key[20] = {0};
sprintf (Key, "%i64d", It->first);

Qdebug ("key =%s,value =%s\n", Key,it->second.c_str ());
}

Method 2 is preferable to a case where 1 keys correspond to multiple value:
typedef std::multimap<boost::uint64_t,std::string>::iterator IT1;
Pair<it1, it1> Res;
res = Mmap_.equal_range (ncallletter);
while (Res.first! = Res.second)
{
cout << res.first->first;
cout << res.first->second;

++res.first;
}

2, the correct use of STL map Erase method, from: http://www.cnblogs.com/kex1n/archive/2011/12/06/2278505.html

    

First of all: The following article is for the use of Windows, because the Std::map erase function of the Windows implementation version is to return a std::map iterator, but the STL standard inside the return value of the function is actually:

There are 3 overloads of the Map.erase:
void Erase (iterator position);
Size_type Erase (const key_type& x);
void Erase (iterator first, iterator last);

So the last example in the code below can only be run under a map under Windows.

The STL map table has a erase method to remove the commands from a map node
EG1:

Map<string,string> maptest;
typedef map<string,string>::iterator ITER;

ITER Iter=maptest.find (key);
Maptest.erase (ITER);
Like above just delete a single node, map shape is not any problem,
But when used in a loop, it is often misused, because the user does not correctly understand the concept of iterator.
An example like the one below is the wrong way of writing.


EG2:

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, which causes the program behavior to be unknown. The reason is that map is an associative container, and if an element has been deleted, the corresponding iterator is invalidated and should not be used again, otherwise it will result in undefined behavior of the program.
You can solve this problem in the following ways:
The correct wording:
1. Locate the next element using the iterator that was previously removed. How STL recommendations are used

For (ITER Iter=maptest.begin (); Iter!=maptest.end ();)
{
cout<<iter->first<< ":" <<iter->second<<endl;
Maptest.erase (iter++);
}
2. The erase () member function returns an iterator to the next element (this method is only used for Windows)

For (ITER Iter=maptest.begin (); Iter!=maptest.end ();)
{
cout<<iter->first<< ":" <<iter->second<<endl;
Iter=maptest.erase (ITER);
}

Find,erase method of correctly using STL map

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.