c++ - 我怎样才能对C++中的最大值进行排序?

标签 c++ sorting

我只想打印出名字和他的最高安打率。我试过 for loop 但它没有对任何东西进行排序,它只是重印每个玩家的平均值。我试过 double average[10] 并且它只返回内存地址。我需要你的建议。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// Structure to hold batting statistics
struct  Player
{
    string name;
    double atBats;
    double hits;
    double average;
};

int main()
{
    ifstream statFile;
    string playerName;
    double bats, hits;
    Player* baseballArray[100];
    Player* aPlayer;
    int numPlayers = 0;
    double largest = 0;
    int temp = 0;

    statFile.open("stats.txt");

    // Continue reading until the end of file
    while (statFile >> playerName)
    {
        statFile >> bats >> hits;

        // Dynamically create a new Player
        aPlayer = new Player;
        aPlayer->name = playerName;
        aPlayer->atBats = bats;
        aPlayer->hits = hits;
        aPlayer->average = aPlayer->hits / aPlayer->atBats;
        // Store the pointer to the Player struct in the array
        baseballArray[numPlayers++] = aPlayer;

        largest = aPlayer->average;

        for (int i = 0; i <= 10; i++) {
            temp = aPlayer->average;
            if (temp > largest)
                largest = temp;
        }

        // Display this information
        cout << aPlayer->name << " has " << aPlayer->atBats << " at bats and "
            << aPlayer->hits << " hits with " << aPlayer->average << " batting average." <<  endl;

        cout << aPlayer->name << " has the highest batting average with " << largest << endl;

    }

最佳答案

您总是在修改 largest ,无论实际值如何。此外,内部 for 循环绝对不执行任何操作。您还在主循环中输出“平均水平最高”的玩家。

如果你想在输入期间跟踪平均最高的玩家,只需存储一个指针或数组索引:

Player* highestAveragePlayer = NULL;

while(numPlayers < 100 && statFile >> playerName >> bats >> hits)
{
    Player* aPlayer = new Player;
    aPlayer->name = playerName;
    aPlayer->atBats = bats;
    aPlayer->hits = hits;
    aPlayer->average = aPlayer->hits / aPlayer->atBats;
    baseballArray[numPlayers++] = aPlayer;

    // Update player with highest average
    if (!highestAveragePlayer || aPlayer->average > highestAveragePlayer->average)
        highestAveragePlayer = aPlayer;
}

// Output AFTER the loop
if (highestAveragePlayer)
{
    cout << highestAveragePlayer->name << " has the highest batting average with "
         << highestAveragePlayer->average << endl;
}

或者,不是在循环中计算它,而是可以在之后用类似 std::max_element 的东西搜索你的玩家数组。来自 <algorithm> :

if (numPlayers > 0)
{
    Player** pIter = std::max_element(baseballArray, baseballArray+numPlayers,
        [](const Player* &a, const Player* &b) {
            return a->average < b->average;
        });
    cout << (*pIter)->name << " has the highest batting average with "
         << (*pIter)->average << endl;
}

关于c++ - 我怎样才能对C++中的最大值进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58229532/

相关文章:

c++ - 我试图在 Mac OS 上运行 Armadillo ,但我不断收到同样的错误

非常量静态成员变量的C++初始化?

javascript - lastclick var 未正确设置

Python 3 列表排序与决胜局

sorting - 按降序对 @Sortable 对象列表进行排序

c++ - 重载 + 运算符以添加数组

c++ - 在 C 中存储对 Lua 值的引用,如何实现?

C++ 模板与继承

c++ - 函数参数中 & 和 * 的区别

php - 如何禁用 SELECT QUERY 对结果进行自动排序