1

我是一个初学者,我解决了一个涉及 10 个吃馅饼的人的练习,其中吃的馅饼数量由用户输入设置。

  1. 第一项任务:显示参赛者人数和吃的馅饼 - 已解决;
  2. 第二项任务:找到吃掉馅饼最多的获胜者并列出所有 - 已解决;
  3. 第三项任务:找到吃掉馅饼最少的失败者并列出所有 - 已解决;
  4. 最后的任务:对参赛者进行排序并按照吃的最多的馅饼的顺序列出他们 - 部分解决。

我做了一切,没有错误或警告(甚至在调试器中跨步/进入显示向量已正确排序),但是当我尝试打印出排序的向量时,控制台屏幕将不再显示任何内容(甚至程序执行完成的标准消息)。

我用带有两个参数的构造函数 Contestant 创建了一个 Contestant 类,并声明了一个对象向量。这是main,类头和类解决方案中的代码:

#include <iostream>
#include <vector>
#include "Contestant.h"
using namespace std;

int main()
{
    cout << "Type the number of pies eaten by each of the 10 contestants:" << endl;
    cout << "#1 #2 #3 #4 #5 #6 #7 #8 #9 #10" << endl;

    vector<Contestant> pie_eaters;
    vector<Contestant*> winners; 
    vector<Contestant*> losers; 


    for (int i=0; i<10; i++)
    {
       int pies_eaten;
       cin >> pies_eaten;
       pie_eaters.emplace_back(i+1, pies_eaten);
       cout << "Contestant number " << i+1 << " ate " << pie_eaters[i].GetPancakes() << endl;
    }
    cout << endl;


    FindWinners(pie_eaters, winners);
    ListWinners(winners);

    FindLosers(pie_eaters, losers);
    ListLosers(losers);


    cout << endl;

    SortPieEaters(pie_eaters);

    ListSortedPieEaters(pie_eaters);


}

类头(已编辑,仅用于排序和打印,在类之外):

#pragma once
#include <iostream>
#include <vector>

class Contestant
{
private: 
    int pancakes_eaten;
    int number;

public:
    Contestant(int number, int pancakes);

    ~Contestant();
    int GetPancakes() const { return pancakes_eaten; }
    int GetNumber() const { return number; }

};


void SortPieEaters(std::vector<Contestant>& pie_eaters);

void ListSortedPieEaters(std::vector<Contestant> const& pie_eaters);

和类解决方案(只是分类和打印出类之外的部分):

#include "Contestant.h"

using namespace std;

Contestant::Contestant(int number, int pancakes) : pancakes_eaten(pancakes), number(number)
{
}

Contestant::~Contestant() {};


void SortPieEaters(vector<Contestant>& pie_eaters)
{
    while(bool swapped=true)
        {
        swapped = false;
           for (int i = 0; i < static_cast<int>(pie_eaters.size()-1); i++)
           {
                if (pie_eaters[i].GetPancakes() < pie_eaters[i + 1].GetPancakes())
                {
                    swap(pie_eaters[i], pie_eaters[i + 1]);
                    swapped = true;
                }

           }
        }
}

void ListSortedPieEaters(vector<Contestant> const& pie_eaters)
{
    cout << "From most pies eaten, to fewest pies eaten, the results are as follows:" << endl;
    for (auto const& c : pie_eaters)
    {
        cout << "Contestant #" << c.GetNumber() << ": ate " << c.GetPancakes() << " pies" <<endl;

    }
}

最后,这里是示例输出: 输出

一切正常,但它不会打印出矢量或警告它有任何问题。尝试过,通过常量或非常量引用传递所有东西,尝试直接在 main 中编写函数体(避免函数),但没有。并且访问向量并打印出内容与赢家和输家向量的情况相同(即使它们是指向饼食者向量的对象元素的指针向量)

我究竟做错了什么?

4

1 回答 1

0

那是因为 while 循环永远不会结束,因为您已经swapped = true在 while 循环条件中初始化了值,swapped当内部 for 循环结束并且重新处理 while 循环时,值变为真。

程序永远不会离开 while 循环。因此,该行永远不会执行

SortPieEaters(pie_eaters); //Executes

ListSortedPieEaters(pie_eaters); //does not execute

你可以做

bool swapped = true;
while(swapped)
{
    swapped = false;
    for (int i = 0; i < static_cast<int>(pie_eaters.size()-1); i++)
    {
        if (pie_eaters[i].GetPancakes() < pie_eaters[i + 1].GetPancakes())
        {
            swap(pie_eaters[i], pie_eaters[i + 1]);
            swapped = true;
        }
    }
}

另外,我建议您通过在Contestant类中重载 < 并改为使用std::sort来将排序逻辑更改为更简单的东西

于 2020-05-08T09:06:53.797 回答