#include <iostream> #include <stdexcept> #include <vector> #include <deque> template <typename T = double, typename CONT = std::vector<T> > class Queue{ private: CONT elements; public: Queue(){}//default constructor T back(){ // return last element return elements.back(); } void push(const T& element){//add an element elements.push_back(element); } void pop(){//erase first element if (elements.empty()) { throw std::out_of_range("Stack<>::pop(): empty stack"); } elements.erase( elements.begin() ); } bool empty(){return elements.empty();} int size(){return elements.size(); } T front(){ if (elements.empty()) { throw std::out_of_range("Stack<>::pop(): empty stack"); } return elements.front();} }; template< typename T> void iterate_Queue(Queue<T>& queue){ while(not queue.empty()) { std::cout << "(" << queue.front()<< ", " << queue.back() << ")"; std::cout << "with size:"<< queue.size() << std::endl; queue.pop(); } std::cout << std::endl; } template< typename T, typename CONT> void iterate_Queue(Queue<T,CONT>& queue){ while(not queue.empty()) { std::cout << "(" << queue.front()<< ", " << queue.back() << ")"; std::cout << "with size:"<< queue.size() << std::endl; queue.pop(); } std::cout << std::endl; } typedef Queue<int> intQueue;// It's common to define typedef's in order to simply int main() { try{ intQueue y; y.push(1); y.push(2); y.push(3); y.push(4); iterate_Queue(y); y.push(15); iterate_Queue(y); Queue<int, std::deque<int> > newExample; newExample.push(10); newExample.push(20); newExample.push(30); newExample.push(40); iterate_Queue(newExample); newExample.push(150); iterate_Queue(newExample); std::cout << "complet dafault example " << std::endl; Queue<> defaultExample; defaultExample.push(1.3); defaultExample.push(2.2); defaultExample.push(3.1); defaultExample.push(4.4); iterate_Queue(defaultExample); defaultExample.push(150); iterate_Queue(defaultExample); } catch (const std::exception& ex) { std::cerr << "Exception: " << ex.what() << std::endl; } return 0; }
read more ->
No comments:
Post a Comment