Advertisement
zhangsongcui

Untitled

Nov 9th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <map>
  4. #include <deque>
  5. #include <thread>
  6. #include <algorithm>
  7.  
  8. int main()
  9. {
  10.     using namespace std;
  11.     deque<int> ids = {4, 7, 0, 7, 1, 0, 1, 2, 1, 2, 6};
  12.     enum { frame_size = 5 };
  13.     typedef map<int, chrono::system_clock::time_point> MAP;
  14.     MAP frame;
  15.     while (!ids.empty())
  16.     {
  17.         int id = ids.front();
  18.         ids.pop_front();
  19.         if (frame.find(id) == frame.end())
  20.         {
  21.             if (frame.size() == frame_size)
  22.             {
  23.                 auto it = min_element(frame.begin(), frame.end(),
  24.                     [] (MAP::const_reference left, MAP::const_reference right)
  25.                     {
  26.                         return left.second < right.second;
  27.                     });
  28.                 cout << it->first << " was erased\n";
  29.                 frame.erase(it);
  30.             }
  31.             frame[id] = chrono::system_clock::now();
  32.         }
  33.         this_thread::sleep_for(chrono::milliseconds(500));
  34.     }
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement