队列快速排序不排序和打印出随机字符串 [C++]
Queue Quick sort not sorting and printing out random strings [C++]
此代码应从用户那里获取 5 个整数并将它们放入列表中并对它们进行排序。我试图证明您可以使用快速排序对队列进行排序,但它打印出随机 strings/variable 名称与字母混合,例如 emp1temp1、9urrentID。我不太熟悉如何使用队列,但我知道 push 添加到队列的前面,pop 删除队列前面的项目,前后查看第一个和最后一个值。
struct PatientDetails {
int ID;
};
queue<PatientDetails> unorderedQueue;
void quickSortIDQueue(queue<PatientDetails> patient);
void addPatient();
int main()
{
for (int i = 0; i < 5; i++) {
addPatient();
}
queue<PatientDetails> Queue;
for (int i = 0; i < unorderedQueue.size(); i++) {
unorderedQueue.push(unorderedQueue.front());
Queue.push(unorderedQueue.front());
unorderedQueue.pop();
}
for (int i = 0; i < unorderedQueue.size(); i++) {
PatientDetails id = Queue.front();
cout << id.ID + " ";
Queue.pop();
}
cout << endl;
quickSortIDQueue(unorderedQueue);
for (int i = 0; i < unorderedQueue.size(); i++) {
PatientDetails id = unorderedQueue.front();
cout << id.ID + " ";
unorderedQueue.pop();
}
cout << endl;
string text;
cin >> text;
return 0;
}
void quickSortIDQueue(queue<PatientDetails> patient) {
queue<PatientDetails> temp1;
queue<PatientDetails> temp2;
queue<PatientDetails> temp3;
PatientDetails pivot = patient.back();
PatientDetails CurrentID;
while (patient.size() > 0) {
CurrentID = patient.front();
patient.pop();
if (CurrentID.ID < pivot.ID) {
temp1.push(CurrentID);
}
else if (CurrentID.ID == pivot.ID) {
temp2.push(CurrentID);
}
else {
temp3.push(CurrentID);
}
if (temp1.size() > 1){
quickSortIDQueue(temp1);
}
if (temp3.size() > 1){
quickSortIDQueue(temp3);
}
}
while (temp1.size() > 0) {
patient.push(temp1.front());
temp1.pop();
}
while (temp2.size() > 0) {
patient.push(temp2.front());
temp2.pop();
}
while (temp3.size() > 0)
{
patient.push(temp3.front());
temp3.pop();
}
}
void addPatient(){
string text = "";
PatientDetails pd;
cin >> text;
pd.ID = stoi(text);
unorderedQueue.push(pd);
}
您看到的主要问题是输出中出现随机垃圾。使用调试器单步执行代码,您可以看到这种情况甚至发生在排序之前,因为您(尝试)在调用排序方法之前输出值。
该问题是由行 cout << id.ID + " ";
.
引起的
你要的是cout << id.ID << " ";
也许这不是用调试器最容易发现的东西;希望你能更好地修复排序本身。
此代码应从用户那里获取 5 个整数并将它们放入列表中并对它们进行排序。我试图证明您可以使用快速排序对队列进行排序,但它打印出随机 strings/variable 名称与字母混合,例如 emp1temp1、9urrentID。我不太熟悉如何使用队列,但我知道 push 添加到队列的前面,pop 删除队列前面的项目,前后查看第一个和最后一个值。
struct PatientDetails {
int ID;
};
queue<PatientDetails> unorderedQueue;
void quickSortIDQueue(queue<PatientDetails> patient);
void addPatient();
int main()
{
for (int i = 0; i < 5; i++) {
addPatient();
}
queue<PatientDetails> Queue;
for (int i = 0; i < unorderedQueue.size(); i++) {
unorderedQueue.push(unorderedQueue.front());
Queue.push(unorderedQueue.front());
unorderedQueue.pop();
}
for (int i = 0; i < unorderedQueue.size(); i++) {
PatientDetails id = Queue.front();
cout << id.ID + " ";
Queue.pop();
}
cout << endl;
quickSortIDQueue(unorderedQueue);
for (int i = 0; i < unorderedQueue.size(); i++) {
PatientDetails id = unorderedQueue.front();
cout << id.ID + " ";
unorderedQueue.pop();
}
cout << endl;
string text;
cin >> text;
return 0;
}
void quickSortIDQueue(queue<PatientDetails> patient) {
queue<PatientDetails> temp1;
queue<PatientDetails> temp2;
queue<PatientDetails> temp3;
PatientDetails pivot = patient.back();
PatientDetails CurrentID;
while (patient.size() > 0) {
CurrentID = patient.front();
patient.pop();
if (CurrentID.ID < pivot.ID) {
temp1.push(CurrentID);
}
else if (CurrentID.ID == pivot.ID) {
temp2.push(CurrentID);
}
else {
temp3.push(CurrentID);
}
if (temp1.size() > 1){
quickSortIDQueue(temp1);
}
if (temp3.size() > 1){
quickSortIDQueue(temp3);
}
}
while (temp1.size() > 0) {
patient.push(temp1.front());
temp1.pop();
}
while (temp2.size() > 0) {
patient.push(temp2.front());
temp2.pop();
}
while (temp3.size() > 0)
{
patient.push(temp3.front());
temp3.pop();
}
}
void addPatient(){
string text = "";
PatientDetails pd;
cin >> text;
pd.ID = stoi(text);
unorderedQueue.push(pd);
}
您看到的主要问题是输出中出现随机垃圾。使用调试器单步执行代码,您可以看到这种情况甚至发生在排序之前,因为您(尝试)在调用排序方法之前输出值。
该问题是由行 cout << id.ID + " ";
.
引起的
你要的是cout << id.ID << " ";
也许这不是用调试器最容易发现的东西;希望你能更好地修复排序本身。