C++ 返回数组中最小元素的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13558081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return index of smallest element in array
提问by gosutag
I'm trying to return the index with the smallest element in an array of integers. Am I missing something? After I put my integers in, it doesn't return the index.
我试图返回整数数组中最小元素的索引。我错过了什么吗?在我放入整数后,它不会返回索引。
UPDATE: I get an error at the end of int main()
about the array stack being corrupted. Thank you. My code is as follows:
更新:我在结束时收到int main()
有关数组堆栈损坏的错误。谢谢你。我的代码如下:
#include <iostream>
#include <conio.h>
using namespace std;
int indexofSmallestElement(double array[], int size);
int main()
{
int size = 10;
double array[10];
for (int i = 0; i <= size; i++)
{
cout << "Enter an integer: " << endl;
cin >> array[i];
}
indexofSmallestElement(array, size);
}
int indexofSmallestElement(double array[], int size)
{
int index = 0;
if (size != 1)
{
int n = array[0];
for (int i = 1; i < size; i++)
{
if (array[i] < n)
{
n = array[i];
index = i;
}
}
}
return index;
}
回答by Nik Bougalis
A number of people have shown you their variant of indexofSmallestElement
. I will include mine along with an explanation of whyI think it's better:
许多人向您展示了他们的indexofSmallestElement
. 我将包括我的以及为什么我认为它更好的解释:
int indexofSmallestElement(double array[], int size)
{
int index = 0;
for(int i = 1; i < size; i++)
{
if(array[i] < array[index])
index = i;
}
return index;
}
You will notice that I do away with the n
variable, which you used to hold the smallest value encountered so far. I did this because, in my experience, having to keep two different things synchronized can be a source of subtle bugs. Even in this simple case, it was the source of multiplebugs, not the least of which was that your n
was declared an int
but you were assigning values of type double
in it.
您会注意到我取消了n
变量,您过去使用该变量保存迄今为止遇到的最小值。我这样做是因为,根据我的经验,必须保持两个不同的东西同步可能会导致微妙的错误。即使在这个简单的例子中,它也是多个错误的根源,其中最重要的是你n
被声明为 anint
但你在其中分配了类型的值double
。
Bottom line: do away with the n
variable and just keep track of one thing: the index.
底线:去掉n
变量,只跟踪一件事:索引。
Update: of course, it goes without saying that with C++17 the onlytruly acceptable answer for this question is to use std::min_element
:
更新:当然,不用说,对于 C++17 ,这个问题唯一真正可以接受的答案是使用std::min_element
:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdint>
#include <array>
#include <vector>
template <typename Iter>
auto indexofSmallestElement(Iter first, Iter last)
{
return std::distance(first,
std::min_element(first, last));
}
template <typename T, std::size_t N>
auto indexofSmallestElement(std::array<T, N> arr)
{
return std::distance(std::begin(arr),
std::min_element(std::begin(arr), std::end(arr)));
}
template <typename T>
auto indexofSmallestElement(std::vector<T> vec)
{
return std::distance(std::begin(vec),
std::min_element(std::begin(vec), std::end(vec)));
}
int main(int, char **)
{
int arr[10] = { 7, 3, 4, 2, 0, 1, 9, 5, 6, 8 };
auto x = indexofSmallestElement(std::begin(arr), std::end(arr));
std::cout
<< "The smallest element in 'arr' is at index "
<< x << ": " << arr[x] << "\n";
std::array<float, 5> fa { 0.0, 2.1, -1.7, 3.3, -4.2 };
auto y = indexofSmallestElement(fa);
std::cout
<< "The smallest element in 'fa' is at index "
<< y << ": " << fa[y] << "\n";
return 0;
}
回答by Coding Mash
It should be n = array[0]
instead of array[0] = n
. It means you are supposing first element of the array to be the smallest in the beginning and then comparing it with others.
它应该n = array[0]
代替array[0] = n
. 这意味着您假设数组的第一个元素在开始时是最小的,然后将其与其他元素进行比较。
Moreover in your loop, you are exceeding the bound of your array. The for loop should run till i < size
and not i <= size
.
此外,在您的循环中,您超出了数组的范围。for 循环应该运行 untili < size
而不是i <= size
。
Your code should be like this..
你的代码应该是这样的..
int indexofSmallestElement(double array[], int size)
{
int index = 0 ;
double n = array[0] ;
for (int i = 1; i < size; ++i)
{
if (array[i] < n)
{
n = array[i] ;
index = i ;
}
}
return index;
}
回答by ashcliffe
inside the loop use array[i] and index = i. Size is the boundary :)
在循环内部使用 array[i] 和 index = i。大小是边界:)