LRU

// LRU.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <queue>
#include<vector>
using namespace std;


void LRU()
{
    vector<int> v;
    cout << "Input Array Length" << endl;
    int L1 = 0;
    int n = 0;
    int pos = 0;
    cin >> L1;
    cout << "Input Page Change Order" << endl;
    while (~scanf("%d",&n))
    {
        if (v.size() < 3)
        {
            int pos = 0;
            for (int i = 0;i<v.size();i++)
            {
                if (v[i] == n)
                {
                    //flag = true;
                    pos = i;
                    std::vector<int>::iterator it = v.begin()+pos;
                    v.erase(it);
                }
            }
            v.push_back(n);

        }
        else 
        {
            bool flag = false;
            int pos = 0;
            for (int i = 0;i<v.size();i++)
            {
                if (v[i] == n)
                {
                    flag = true;
                    pos = i;
                }
            }
            if (flag)
            {
                std::vector<int>::iterator it = v.begin()+pos;
                v.erase(it);
                v.push_back(n);
            }
            else
            {
                v.erase(v.begin());
                v.push_back(n);
            }
        }
        for (int i = 0;i<v.size();i++)
        {
            cout << v[i] << " ";
        }
        cout << endl;

    }

}
int main(int argc, char* argv[])
{
    //printf("Hello World!
");
    LRU();
    return 0;
}
原文地址:https://www.cnblogs.com/yifi/p/6672677.html