kleiner Quelltext mit Problem
-
ich möchte das erste Element (Funktion pop) löschen weiß aber nicht wie
#include <iostream> #include <cstdlib> using namespace std; template <class T, int limit> class Queue { public: Queue():n_(0) { } bool empty() const { return n_ == 0; } void push(const T& t) { elements_[n_++] = t; } void pop() { T temp = elements_[--n_]; elements_[0] = temp; cout << "n_ = " << n_ << endl; } T front() { return elements_[0]; } T back() { return elements_[n_ - 1]; } private: int n_; int elements_[limit]; }; int main() { Queue<char,3> q; cout << (q.empty() ? "Schlange leer" : "Schlange nicht leer") << endl; q.push('a'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('b'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('c'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('d'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('e'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('f'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('h'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; cout << (q.empty() ? "Schlange leer" : "Schlange nicht leer") << endl; system("pause"); return 0; }
-
Geht nur wenn du mit new und delete immer wieder neue Arraygrößen angibts. Besser wäre ne Liste um die Elemente zu speichern.
-
du könntest auch alle elemente in das vorherige kopieren, oder das array als zirkuläre liste betrachten.
-
es funktioniert jetzt soweit nur wird die ganze Sache verschoben aber so soll es nicht getan werden ...
#include <iostream> #include <cstdlib> using namespace std; template <class T, int limit> class Queue { public: Queue():n_(0) { } bool empty() const { return n_ == 0; } void push(const T& t) { elements_[n_++] = t; } void pop() { for(int i = 0; i < n_ - 1; i++) elements_[i] = elements_[i+1]; elements_[n_--]; } T front() { return elements_[0]; } T back() { return elements_[n_ - 1]; } private: int n_; int elements_[limit]; }; int main() { Queue<char,3> q; cout << (q.empty() ? "Schlange leer" : "Schlange nicht leer") << endl; q.push('a'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('b'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('c'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('d'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('e'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('f'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.pop(); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; q.push('h'); cout << "Anfang: " << q.front() << " Ende: " << q.back() << endl; cout << (q.empty() ? "Schlange leer" : "Schlange nicht leer") << endl; system("pause"); return 0; }
-
elements_[n_--];
Was soll das bringen?
-
Will sagen: der Entwurf ist echt schlecht, wenn du durch push Elemente einfügst verlässt du irgendwann die Arraygrenzen -> Peng
-
Wenn Du intern ein statisches Feld verwenden willst, solltest Du am besten den Anfangs- und End-Index so verwalten, dass Du eine Art zirkulären Buffer hast. Wie Glamdrink richtig bemerkt hat, sollte man es auch nicht unbedingt zulassen, dass mit unbekanntem Speicher rumgemacht wird.
In diesem Sinne:#include <iostream> #include <stdexcept> using namespace std; template <class T, int limit> class Queue { public: Queue():_count(0),_front(0),_back(-1) {} inline int getCapacity() const {return limit;} inline int getCount() const {return _count;} inline T getFront() const //throw(length_error) { if(_count==0) throw length_error("Nix getFront! Die Schlange ist leer!"); return _elements[_front]; } inline T getBack() const //throw(length_error) { if(_count==0) throw length_error("Nix getBack! Die Schlange ist leer!"); return _elements[_back]; } inline bool isEmpty() const {return _count == 0;} void push(const T& t) //throw(length_error) { if(_count>=limit) throw length_error("Nix push! Die Schlange ist voll!"); _back = (_back+1) % limit; _elements[_back] = t; ++_count; } T &pop() //throw(length_error) { if(_count==0) throw length_error("Nix pop! Die Schlange ist leer!"); T &result = _elements[_front]; _front = (_front+1) % limit; --_count; return result; } private: int _count,_front,_back; T _elements[limit]; }; int main() { Queue<char,3> q; cout << (q.isEmpty() ? "Schlange leer" : "Schlange nicht leer") << endl; try { cout << q.getFront() << endl; } catch(const exception &ex) { cout << ex.what() << endl; } try { cout << q.getBack() << endl; } catch(const exception &ex) { cout << ex.what() << endl; } q.push('a'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('b'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.pop(); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('c'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('d'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.pop(); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.pop(); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('e'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('f'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.pop(); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; q.push('h'); cout << "Anfang: " << q.getFront() << " Ende: " << q.getBack() << endl; cout << (q.isEmpty() ? "Schlange leer" : "Schlange nicht leer") << endl; try { q.push('a'); q.push('b'); q.push('c'); q.push('d'); } catch(const exception &ex) { cout << ex.what() << endl; } try { cout << q.pop() << endl; cout << q.pop() << endl; cout << q.pop() << endl; q.push('d'); q.push('e'); cout << q.pop() << endl; cout << q.pop() << endl; q.push('f'); cout << q.pop() << endl; cout << q.pop() << endl; } catch(const exception &ex) { cout << ex.what() << endl; } cin.get(); }