页面置换算法LRU
Posted Keep--Silent
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了页面置换算法LRU相关的知识,希望对你有一定的参考价值。
文章目录
说明
LRU: least recently used
算法:单词查询修改O(n)
代码
#include <bits/stdc++.h>
using namespace std;
void print(vector<int> v)
cout << "";
for (auto t : v)
if(t<0)break;
cout << t << ",";
cout << "";
int main()
/*
4
2 0 2 9 3 4 2 8 2 4 8 4 5
4
1 8 1 7 8 2 7 2 1 8 3 8 2 1 3 1 7 1 3 7
*/
int n, x;
cout << "输入页面数" << endl;
cin >> n;
vector<int> v(n, -1),v2;
cout << "输入页号" << endl;
while (cin >> x)
cout<<x<<":";
int flag=0;
v2=v;
for(auto &t:v)
if(x==v[0])
flag=1;
break;
swap(x,t);
if(t<0)break;
if(flag||x==v[0])cout<<"hit: ";
else cout<<"miss: ";
print(v2);
cout<<"->";
print(v);
cout<<endl;
return 0;
测试数据
输入
4
2 0 2 9 3 4 2 8 2 4 8 4 5
输出
输入页面数
4
输入页号
2 0 2 9 3 4 2 8 2 4 8 4 5
2:miss: ->2,
0:miss: 2,->0,2,
2:hit: 0,2,->2,0,
9:miss: 2,0,->9,2,0,
3:miss: 9,2,0,->3,9,2,0,
4:miss: 3,9,2,0,->4,3,9,2,
2:hit: 4,3,9,2,->2,4,3,9,
8:miss: 2,4,3,9,->8,2,4,3,
2:hit: 8,2,4,3,->2,8,4,3,
4:hit: 2,8,4,3,->4,2,8,3,
8:hit: 4,2,8,3,->8,4,2,3,
4:hit: 8,4,2,3,->4,8,2,3,
5:miss: 4,8,2,3,->5,4,8,2,
创作打卡挑战赛
赢取流量/现金/CSDN周边激励大奖
以上是关于页面置换算法LRU的主要内容,如果未能解决你的问题,请参考以下文章