在数组中查找最小值

Find min value in array

本文关键字:最小值 查找 数组      更新时间:2023-10-16

我有两个数组:

int playerSums[9] = { };
string playerNames[9] = { };

我正在尝试获取数组playerSums中的最小值,以及该值的数组索引

以下是我迄今为止所尝试的:

if (playerNames[index] == "End" || playerNames[index] == "end") {
    int lowestValue = playerSums[0];
    for (i = 1; i < sizeof(playerSums) / sizeof(playerSums[0]); i++) {
        if (playerSums[i] < lowestValue || lowestValue != 0)
            lowestValue = playerSums[i]; 
    }
    cout << index[playerNames] << " had the lowest values and got the sum ";
    cout << lowestValue << endl;
}

例如,如果只有3名玩家在玩,即数组中只有3个元素被填充(其余元素等于零),我如何找到并显示数组playerSums中的最小值?

我需要索引来显示得到最小值的玩家的名字。

您可以使用标头<algorithm>中声明的标准算法std::min_element来查找具有最小和的元素。例如

#include <algorithm>
int *min = std::min_element( playerSums, playerSums + 3 );
std::cout <<  playerNames[min - playerSums] 
          << " had the lowest values and got the sum " << *min
          << std::endl;

可以使用报头<iterator> 中声明的标准函数std::beginstd::endstd::distance来写入相同的内容

#include <algorithm>
#include <iterator>
int *min = std::min_element( std::begin( playerSums ), std::end( playerSums ) );
std::cout <<  playerNames[ std::distance( playerSums, min )] 
          << " had the lowest values and got the sum " << *min
          << std::endl;

您可以编写类似于该算法的自己的函数,而不是使用该算法。例如

size_t min_sum( const int playerSums[], size_t n )
{
   size_t min = 0;
   for ( size_t i = 1; i < n; i++ )
   {
      if ( playerSums[min] < playerSums[i] ) min = i;
   }
   return min;
}
size_t min = min_sum( playerSums, sizeof( playerSums ) / sizeof( *playerSums )  );
std::cout <<  playerNames[min] 
          << " had the lowest values and got the sum " << playerSums[min]
          << std::endl;

如果你需要跳过数组中等于零的元素,那么函数将看起来像

size_t min_sum( const int playerSums[], size_t n )
{
   size_t min = 0;
   while ( min < n && playerSums[i] == 0 ) ++min;
   for ( size_t i = min; i < n; i++ )
   {
      if ( playerSums[min] < playerSums[i] ) min = i;
   }
   return min;
}
size_t min = min_sum( playerSums, sizeof( playerSums ) / sizeof( *playerSums )  );
if ( min != sizeof( playerSums ) / sizeof( *playerSums ) )
{   
    std::cout <<  playerNames[min] 
              << " had the lowest values and got the sum " << playerSums[min]
              << std::endl;
}

与在lowestValue中存储最低值的方法相同,将索引存储在一个变量中,比如lowestValueIndex。此外,移除外部if并将其移动到for循环内部:

if(playerNames[i] == "End" || playerNames[i] == "end")
    break;

这样,你将确保只有正在比赛的球员才会被处理。此外,您将不再需要检查最低值是否为零。所以代码看起来像:

int lowestValue = playerSums[0];
int lowestValueIndex = 0;
for (int i = 1; i < sizeof(playerSums)/sizeof(playerSums[0]); ++i)
{
    if(playerNames[i] == "End" || playerNames[i] == "end")
        break;
    if (playerSums[i] < lowestValue)
    {
            lowestValue = playerSums[i];
            lowestValueIndex = i;
    }
}
cout << index[playerNames] << " had the lowest values and got the sum "
     << lowestValue << endl;

需要注意的是,使用一个可以增长的标准数组来简化它(比如vector):

std::vector<std::string> playerNames;
std::vector<int> playerSums;
for (int i = 1; i < playerSums.size(); ++i)
{
    if (playerSums[i] < lowestValue)
    {
            lowestValue = playerSums[i];
            lowestValueIndex = i;
    }
}
cout << index[playerNames] << " had the lowest values and got the sum "
     << lowestValue << endl;

通常,最简单的解决方案是使用标准库,例如

auto it = std::min_element(std::begin(playerSums), std::end(playerSums));
std::size_t index = std::distance(std::begin(playerSums), it);

现在您可以通过取消引用迭代器it:来获得最小值

int lowestValue = *it;

如果您只想迭代数组中前3个元素,那么您可以执行以下操作:

auto first = std::begin(playerSums);
auto it = std::min_element(first, std::next(first, 3));
std::size_t index = std::distance(first, it);

注意:更喜欢std::next而不是普通的指针算法(例如playerSums + 3),因为它更通用(适用于所有迭代器类型)。

首先调整for循环条件。我不确定你以前是否定义过我,所以也许你忘了。第二,停止条件i<sizeof(palierSums)就足够了。此外,您只需要存储数组中最低playerSumes的索引。if条件也有太多的内容。如果lowestValue不为零,您将始终更改该值,除非lowestValue恰好为零,否则这似乎是不对的。

int lowestValue = playerSums[0];
int resultIndex = 0;
for(int i = 1; i < sizeof(playerSums) / sizeof(playerSums[0]); i++) {
  if(playerSums[i] < lowestValue) {
    lowestValue = playerSums[i];
    resultIndex = i;
  }
}
cout << playerNames[resultIndex] << "blabla" << lowestValue; // instead of lowestValue you could also do playerSums[resultIndex] ofcourse.

让我知道这是否有效

当您更改变量的值时,您知道分配给lowestValue的元素的索引,所以只需将该索引保存在变量中(例如,index),这样当您完成后,index就可以分配最后一个值的索引。