Ich habe kürzlich mit dynamischer Zuweisung und Warteschlange gespielt. Denken Sie daran, dass ich mit den Datenstrukturen ziemlich neu bin und mir selbst beigebracht habe, indem ich mir YouTube-Tutorials angesehen und einige Tutorials von Websites gelesen habe. Verzeihen Sie meine Ignoranz, wenn ich nicht alles über dieses Thema weiß. Während ich jedoch mit der Implementierung eines Zuweisungsoperators für meine Warteschlange herumspielte, habe ich irgendwo in seinem Code einen Fehler gemacht. Sie sehen, wenn ich versuche, und die Betreiber Zuordnung verwendenZuweisungsoperator, der alles mit Ausnahme des freien Speicherplatzes im Array kopiert
heißt
Queue names2;
names2 = names;
der Zuweisungsoperator kopiert alles bereits in der Warteschlange mit Ausnahme der leeren Elemente des Arrays. Implementiere ich meinen Kopierkonstruktor oder meinen Zuweisungsoperator falsch, was dazu führt, dass die leere nicht verwendete Kapazität nicht zur neu erstellten Warteschlange hinzugefügt wird?
main.cpp
#include "Queue.h";
int main()
{
Queue names;
names.enqueue("A");
names.enqueue("B");
names.enqueue("C");
names.dequeue();
names.enqueue("D");
names.showFullQueue();
Queue names2;
names2 = names;
names2.showFullQueue();
names2.enqueue("Tom");
names2.enqueue("Alice");
names2.showFullQueue();
return 0;
}
Queue.h
#ifndef Queue_H
#define Queue_H
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
class Queue {
public:
Queue(); //Defualt costructor
Queue(const Queue& source); //Copy constructor
~Queue(); //Destructor
Queue& operator=(const Queue& source); //Assignament Operator //NOT WORKING
void enqueue(string value); // add item to queue
string dequeue(); // remove item from queue
void showFullQueue() const;
//memory allocation
void memory(int currentCapacity);
private:
void shift();
string * arr;
const static int front = 0;
int back;
int capacity;
int usedCapacity;
};
#endif // !Queue_H
Queue.cpp
#include "Queue.h"
Queue::Queue()
{
back = -1;
capacity = 1;
usedCapacity = 0;
arr = new string[capacity];
}
Queue::Queue(const Queue & source)
{
cout << "Invoking copy constructor" << endl;
this->back = source.back;
this->capacity = source.capacity;
this->usedCapacity = source.usedCapacity;
arr = new string[source.capacity];
copy(source.arr, source.arr + usedCapacity, this->arr);
}
Queue::~Queue()
{
delete[] arr;
}
Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
return *this;
cout << "Invoking Assignament operator" << endl;
Queue temp(source);
swap(temp.back, back);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, capacity);
swap(temp.arr, arr);
return *this;
}
void Queue::enqueue(string value)
{
++back;
arr[back] = value;
usedCapacity++;
memory(usedCapacity);
}
string Queue::dequeue()
{
string returnValue = arr[front];
shift();
usedCapacity--;
memory(usedCapacity);
return returnValue;
}
void Queue::showFullQueue() const
{
cout << "Front: " << front << endl;
cout << "Back: " << back << endl;
for (int i = 0; i < capacity; i++)
{
cout << arr[i] << ", ";
}
cout << endl;
}
void Queue::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3)/2 + 1;
string * arr2 = new string[newCapacity];
copy(arr, arr + usedCapacity, arr2);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
else
cout << "allocation error";
}
void Queue::shift()
{
for (int i = front; i <= back; i++)
{
arr[i] = arr[i + 1];
}
--back;
}
Jede Hilfe wäre sehr sein appreci ated. Danke im Voraus.
Bitte entfernen Sie alle und setzen nur die [MCVE] –
Sie würden sich eine ganze Menge Stress gespeichert, wenn Sie 'std :: vector verwendet' anstatt 'std :: string *' –
WhiZTiM
Ja Ich wollte nur mit dynamischen String-Arrays herumspielen. Ich werde beim nächsten Mal Vektoren verwenden, aber jetzt wollte ich nur wissen, wie man eine Warteschlange implementiert, ohne Vektoren zu verwenden. –