深海重苹果 2021-05-21 21:52 采纳率: 100%
浏览 95
已采纳

C++报错map,如何解决?

c++使用map的时候出现了这个,请问是什么问题?

附一下我的代码:

#include<iostream>
#include<time.h>
#include<string>
#include<map>
using namespace std;
#define random(x) rand()%(x)
int main() 
{
	srand(time(NULL));
	multimap<int, int>insstream;
	int count = 0;
	while (count < 320) {
		int m = random(319);
		count++;
		insstream.insert(pair<int, int>(count,m+1));
		int m1 = random(m + 1);
		count++;
		insstream.insert(pair<int, int>(count, m1));
		count++;
		insstream.insert(pair<int, int>(count, m1+1));
		int m2 = random(319 - (m1 + 2) + 1) + m1 + 2;
		count++;
		insstream.insert(pair<int, int>(count, m2));
	}
	//检测指令流
	//for (auto ins = insstream.begin(); ins != insstream.end(); ins++)
		//cout << ins->second << " ";
	multimap<int, int>pagestream;
	for (auto ins = insstream.begin(); ins != insstream.end(); ins++)
		pagestream.insert(pair<int, int>(ins->first, ins->second/10));
	//检测页面流
	for (auto page = pagestream.begin(); page != pagestream.end(); page++)
		cout << page->second << " ";
	cout << endl;
	string choose;
	cout << "*************************************************" << endl;
	cout << "欢迎使用本程序~" << endl;
	cout << "请选择您需要的算法(FIFO、LRU、OPT、LFR、CLOCK):" << endl;
	cin >> choose;
	int allocation = random(6 - 3 + 1) + 3;
	cout << "本次分配的物理块数为:" << allocation << "块!" << endl;
	map<int, int>run;
	if (choose == "FIFO" || choose == "fifo") {
		run.insert(pair<int, int>(pagestream.find(1)->first, pagestream.find(1)->second));
		for (auto page = pagestream.begin(); page != pagestream.end(); page++)
		{
			for (auto r = run.begin(); r != run.end() && r->second != page->second; r++)
			{
				if (r == run.find(run.size())) {
					if (run.size() < allocation) {
						run.insert(pair<int, int>(page->first, page->second));
					}
					else if (run.size() == allocation) {
						run.erase(run.find(1));
						run.insert(pair<int, int>(page->first, page->second));
					}
				}
			}
			for (auto r = run.begin(); r != run.end(); r++)
				cout << r->second << " ";
			cout << endl;
		}
	}
	else if (choose == "LRU"|| choose == "lru") {

	}
	else if (choose == "OPT"|| choose == "opt") {

	}
	else if (choose == "LFR"|| choose == "lfr") {

	}
	else if(choose == "CLOCK" || choose == "clock"){
		
	}
	else {
		cout << "您的输入有误!请输入:FIFO、LRU、OPT、LFR、CLOCK中的一个!" << endl;
	}
	cout << "感谢您的使用~祝您生活愉快!" << endl;
	cout << "*************************************************" << endl;
}
  • 写回答

2条回答 默认 最新

  • 深海重苹果 2021-05-22 11:48
    关注

    这里我找到了问题所在,在一个容器的迭代器中不能直接删除或者插入该容器的元素,否则会破坏迭代器结构

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 猫叔大鸭梨 2021-05-21 22:51
    关注

    我试了,没报错。。正常运行

     

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab计算100000*100000的矩阵运算问题:
  • ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
  • ¥16 需要完整的这份订单所有的代码,可以加钱
  • ¥30 写一个带界面控制的机房电脑一键开机关机并且实时监控的软件
  • ¥15 Stata数据分析请教
  • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
  • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料
  • ¥15 51单片机的外部中断,按下按键后不能切换到另一个模式
  • ¥15 java连接sqlserver有问题
  • ¥15 yolov8 如何调cfg参数