c++ - 为什么不能使用带范围的for循环打印对象 vector 的内容?

标签 c++ vector output cout ranged-loops

我是一个初学者,我解决了一个涉及10个饼食者的练习,并通过用户输入设置了所吃的饼数。

  • 第一项任务:显示参赛者人数和被吃掉的馅饼-已解决;
  • 第二项任务:查找被吃掉最多馅饼的获奖者并列出所有-解决;
  • 第三项任务:找到被吃掉的馅饼最少的失败者,并全部列出-解决了;
  • 最后任务:对参赛者进行排序,并按照最多派的顺序列出他们
    最少吃的馅饼-部分溶解。

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

    我用构造函数Contestant创建了一个带有两个参数的Contestant类,并声明了一个对象 vector 。这是main,类 header 和类解决方案中的代码:
    #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;
    
        }
    }
    

    最后,这是示例输出:
    Output

    一切正常,但不会打印出 vector 或警告它有任何问题。尝试过类似通过常量或非常量引用传递的所有内容,尝试直接在main(避免使用函数)中编写函数的主体,但是什么也没做。
    而且访问 vector 并打印出内容与获胜者和输者 vector 相同(即使它们是指向食堂动物 vector 的对象元素的指针的 vector )

    我究竟做错了什么?

    最佳答案

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

    该程序永远不会离开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来将排序逻辑更改为更简单的方式

    关于c++ - 为什么不能使用带范围的for循环打印对象 vector 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61675421/

    相关文章:

    c++ - 将指针推回 vector C++

    php - 如何在 PHP mail() 中将文本设为粗体?

    谁能检查程序并告诉我如何获得正确的输出?

    使用工厂的 C++ 接口(interface)继承

    c++ - 访问 COM 对象中的不透明数据成员

    c++ - 为什么我在构建结构时在 Visual C++ 2008 中收到这些警告?

    c++ - 如果我不想管理我指向的对象的内存,我应该使用 unique_ptr 吗?

    c - 获取结构体 vector 的索引号

    C++03 根据成员函数值查找元素

    arrays - Swift - 将数组写入文本文件