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

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
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测