我有两个数组:
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个播放器,我如何找到并显示数组playerSums
中的最小值,即只填充数组的3个元素(其余部分)元素等于零)?
我需要索引来显示获得最小值的玩家的名字。
答案 0 :(得分:3)
您可以使用标头std::min_element
中声明的标准算法<algorithm>
来查找最小总和的元素。例如
#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::begin
中声明的标准函数std::end
,std::distance
和<iterator>
来编写
#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( 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( 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;
}
答案 1 :(得分:3)
与在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;
答案 2 :(得分:3)
通常最简单的解决方案是使用标准库,例如
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
),因为它更通用(适用于所有迭代器类型)。
答案 3 :(得分:2)
当您更改该变量的值时,您知道分配给lowestValue
的元素的索引,因此只需将该索引保存在变量中(例如,index
),以便在完成时{ {1}}具有指定的最后一个值的索引。
答案 4 :(得分:2)
首先调整你的for循环条件。我不确定你之前是否定义过我,也许你忘记了。第二个停止条件i&lt; sizeof(palyerSums)就足够了。此外,您只需要存储数组中最低的playerSums的索引。 if条件也有太多东西。如果minimumValue不为零,您将始终更改该值,除非minimumValue正好为零,否则这似乎不正确。
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.
如果有效,请告诉我