【问题标题】:Why can't I print contents of vector of objects using a ranged for loop?为什么我不能使用范围 for 循环打印对象向量的内容?
【发布时间】:2020-05-08 08:49:55
【问题描述】:

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

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

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

我用构造函数 Contestant 和两个参数创建了一个 Contestant 类,并声明了一个对象向量。这是main、class header和class solution中的代码:

#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);

和Class解决方案(只是排序和打印出来的部分,在类之外):

#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

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

我做错了什么?

【问题讨论】:

    标签: c++ vector output cout ranged-loops


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!它奏效了,并且再次证明,作为初学者,我必须始终首先关注最简单的事情,因为那是我的问题所在。我试图简化事情,这就是我犯错误的地方。我对这个问题投了赞成票,但我是新手,不会注册。关于你的建议,是更快还是有什么好处?
    • STL 函数通常更快,并且针对它们正在执行的确切任务进行了优化。例如,std::sort 的一些变体(取决于编译器实现)使用 Introsort 或 quicksort,“Introsort 是一种混合排序算法,它使用三种排序算法来最小化运行时间,即快速排序、堆排序和插入排序”。此外,代码看起来更简单:)
    • 谢谢!这对我来说又是一个进步! :D
    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多