C ++列表显示表中的最后3项

时间:2018-04-17 14:32:56

标签: c++ arrays class object constructor

嘿,我有一张球队的表格,上面有他们所拥有的名字和分数,我试图弄清楚如何在表格中显示最少得分的最后3支球队?

显示所有团队,我希望它只显示表格中的最后3个,但不知道该怎么办。

这些是我的配件

字符串GetName int GetPoints

int low = 1000;

for (int i = 0; i < numTeams; i++)
{
    if (league[i].GetPoints() < lowest)
    {
        lowest = league[i].GetPoints();
    }
}

for (int i = 0; i < numTeams; i++)
{
    if (league[i].GetPoints() == lowest)
    {
        cout << "\tThe lowest goals against is: " << league[i].GetName() << endl;
    }
}

3 个答案:

答案 0 :(得分:3)

实际上,如果您要在打印前对数据进行排序,则不需要变量lowest

#include <algorithm>

// Sort using a Lambda expression.
std::sort(std::begin(league), std::end(league), [](const League &a, const League &b) {
    return a.GetPoints() < b.GetPoints();   
});

int last = 3;

for (int i = 0; i < last; i++)
{
    cout << "\tThe lowest goals against is: " << league[i].GetName() << endl;
}

答案 1 :(得分:0)

你可能可以从排序数组开始

#include <algorithm>

std::array<int> foo;
std::sort(foo.begin(), foo.end());

然后从最后一个元素迭代到最后一个元素 - 3.(你可以使用反向迭代器)

for (std::vector<int>::reverse_iterator it = v.rend() ; it != v.rend() + 3; 
it++) {
    //Do something
}

或使用自动

for (auto it = v.rend() ; it != v.rend() + 3; ++it) {
    //Do something
}

答案 2 :(得分:-1)

在我的示例中,我创建了测试类(TestTeam),以便为任务中的对象实现几个重要的方法。 我使用std::sort方法对对象容器进行排序,默认情况下std :: sort通过less(&lt;)操作比较对象,所以我为TestTeam对象重写了运算符<

bool operator < ( const TestTeam& r) const
{
    return GetPoints() < r.GetPoints();
}

我们也可以传递另一个比较方法或lambda方法作为第三个参数,如下面的答案所示:

std::sort(VecTeam.begin(), VecTeam.end(), [](const TestTeam& l, const TestTeam& r)
                  {
                      return l.GetPoints() < r.GetPoints();
                  });

例如,当我们使用全局方法进行比较时:

bool CompareTestTeamLess(const TestTeam& l, const TestTeam& r)
{
    return l.GetPoints() < r.GetPoints();
}           

//...
// some code
//...     

// In main() we use global method to sort
std::sort(VecTeam.begin(), VecTeam.end(), ::CompareTestTeamLess);

您可以使用vector作为容器来尝试我的代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

// Test class for example
class TestTeam
{
public:
    TestTeam(int16_t p, const std::string& name = "Empty name"):mPoints(p), mName(name)
    {
    };

    int16_t GetPoints() const {return mPoints;}
    const std::string& GetName() const {return mName;}

    void SetName( const std::string& name ) {mName=name;}

    bool operator < ( const TestTeam& r) const
    {
        return GetPoints() < r.GetPoints();
    }

private:
    int16_t mPoints;
    std::string mName;

};

int main(int argc, const char * argv[])
{
    const uint32_t COUNT_LOWEST_ELEMENTS_TO_FIND = 3;

    // Fill container by test data with a help of non-explicit constructor to solve your task
    std::vector<TestTeam> VecTeam {3,5,8,9,11,2,14,7};

    // Here you can do others manipulations with team data ...

    //Sort vector by GetPoints overloaded in less operator. After sort first three elements will be with lowest points in container
    std::sort(VecTeam.begin(), VecTeam.end());

    //Print results as points - name
    std::for_each( VecTeam.begin(), VecTeam.begin() + COUNT_LOWEST_ELEMENTS_TO_FIND, [] (TestTeam el)
                      {
                          std::cout << el.GetPoints() << " - " << el.GetName() << std::endl;
                      } );
}

我只为测试类TestTeam实现了对象的测试逻辑。 如果您尝试启动该程序,您可以获得下一个结果:

2 - Empty name
3 - Empty name
5 - Empty name
Program ended with exit code: 0