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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵