Advertisement
Guest User

Templates&Exclusions

a guest
May 27th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template <int N, class T>
  7. class queue {
  8.     private:
  9.         vector<T> vec;
  10.         int max_size;
  11.     public:
  12.         queue()
  13.         {
  14.             max_size = N;
  15.         }
  16.         class overflow {};
  17.         class empty {};
  18.  
  19.         void push(T value)
  20.         {
  21.             if (vec.size() == max_size)
  22.                 throw overflow();
  23.             else
  24.                 vec.push_back(value);
  25.         }
  26.  
  27.         T pop()
  28.         {
  29.             if (vec.empty()) {
  30.                 throw empty();
  31.             } else {
  32.                 T value = vec[0];
  33.                 vec.erase(vec.begin() + 0);
  34.  
  35.                 return value;
  36.             }
  37.         }
  38. };
  39.  
  40. int main()
  41. {
  42.     queue<2, int> q;
  43.  
  44.     try {
  45.         cout << q.pop() << endl;
  46.     }
  47.     catch (queue<2, int>::empty err) {
  48.         cout << " ***Queue is Empty ***" << endl;
  49.     }
  50.  
  51.     q.push(42);
  52.     q.push(420);
  53.  
  54.     try {
  55.         q.push(228);
  56.     }
  57.     catch (queue<2, int>::overflow err) {
  58.         cout << " ***Queue Overflow ***" << endl;
  59.     }
  60.  
  61.     cout << q.pop() << endl;
  62.     cout << q.pop() << endl;
  63.  
  64.     q.push(150); // see?
  65.     q.push(500);
  66.     //q.push(700); NO
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement