首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在数组中查找最小值

在数组中查找最小值
EN

Stack Overflow用户
提问于 2014-10-02 17:36:34
回答 5查看 24.1K关注 0票数 4

我有两个数组:

代码语言:javascript
运行
复制
int playerSums[9] = { };
string playerNames[9] = { };

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

到目前为止,我尝试过的是:

代码语言:javascript
运行
复制
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;
}

如何在数组playerSums中找到和显示最小值(例如,只播放3名播放器,即只填充数组的3个元素(其余元素等于0)?

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-10-02 17:53:08

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

代码语言:javascript
运行
复制
#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;

也可以使用标头中声明的标准函数std::beginstd::endstd::distance编写。

代码语言:javascript
运行
复制
#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;

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

代码语言:javascript
运行
复制
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;

如果需要跳过等于零的数组元素,则该函数如下所示

代码语言:javascript
运行
复制
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;
}
票数 9
EN

Stack Overflow用户

发布于 2014-10-02 17:53:21

就像在lowestValue中存储最低值一样,将索引存储在变量中,比方说,lowestValueIndex。另外,移除外部if并将其移动到for循环中:

代码语言:javascript
运行
复制
if(playerNames[i] == "End" || playerNames[i] == "end")
    break;

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

代码语言:javascript
运行
复制
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):

代码语言:javascript
运行
复制
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;
票数 4
EN

Stack Overflow用户

发布于 2014-10-02 17:53:38

和往常一样,最简单的解决方案是使用标准库。

代码语言:javascript
运行
复制
auto it = std::min_element(std::begin(playerSums), std::end(playerSums));
std::size_t index = std::distance(std::begin(playerSums), it);

现在,您可以通过取消迭代器it来获得min值。

代码语言:javascript
运行
复制
int lowestValue = *it;

如果只想迭代数组中的前3个元素,那么可以这样做:

代码语言:javascript
运行
复制
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),因为它更通用(适用于所有迭代器类型)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26166337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档