Bugs encountered with the map container for C + + (Map/set iterator not dereferencable) __c++

Source: Internet
Author: User
from:http://blog.csdn.net/mrknight/article/details/9418469

I just happened to have met myself. This error. Actually, it's very low-level.

Recently using C + +, the map container was used in the program, prompting for error Map/set iterator not dereferencable.

At first it was doubtful that the iterator had not lifted the reference ...

The final discovery is that the element indicated by the iterator is empty (that is, end), but the program has a bug in the loop that uses the iterator, which causes an empty iterator to be used because the existing condition is sufficiently constrained.

Map<string, pair<double, double>>::iterator it = Shotlist.find

(shotid);
					
while (It->first.find (String ("shot") + ID)!= string::npos)
{
	if It->second.first <= start && It->second.second >= start| |
	It->second.first <= End && it->second.second >= end)
	{
		Shotresultlist.push_back (it-> a);

	++it;
}

After modification

Map<string, pair<double, double>>::iterator it = Shotlist.find

(shotid);
					
while (it!= shotlist.end () &&
	it->first.find (String ("shot") + ID)!= string::npos)
{
	if (it- >second.first <= start && it->second.second >= start| |
	It->second.first <= End && it->second.second >= end)
	{
		Shotresultlist.push_back (it-> a);

	++it;
}


You can see that the error code does not cause an error because it!= shotlist.end () condition constraints.

Related Article

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.