Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- template <int N, class T>
- class queue {
- private:
- vector<T> vec;
- int max_size;
- public:
- queue()
- {
- max_size = N;
- }
- class overflow {};
- class empty {};
- void push(T value)
- {
- if (vec.size() == max_size)
- throw overflow();
- else
- vec.push_back(value);
- }
- T pop()
- {
- if (vec.empty()) {
- throw empty();
- } else {
- T value = vec[0];
- vec.erase(vec.begin() + 0);
- return value;
- }
- }
- };
- int main()
- {
- queue<2, int> q;
- try {
- cout << q.pop() << endl;
- }
- catch (queue<2, int>::empty err) {
- cout << " ***Queue is Empty ***" << endl;
- }
- q.push(42);
- q.push(420);
- try {
- q.push(228);
- }
- catch (queue<2, int>::overflow err) {
- cout << " ***Queue Overflow ***" << endl;
- }
- cout << q.pop() << endl;
- cout << q.pop() << endl;
- q.push(150); // see?
- q.push(500);
- //q.push(700); NO
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement