查找存储最小值的元素
Find the element where the minimum value is stored
我有一个非常微不足道的问题,但我在互联网上找不到任何涵盖它的内容。我需要在存储最小值的一维数组中找到索引。到目前为止,我找到的所有内容都只告诉您最小值的值。我尝试使用的代码是:
int min_element_loc (double a[]){
double first, last;
first = a[0];
last = a[255];
for(int i = 0;i<256;i++)
{
if (first==last) return i;
double smallest = first;
while (++first!=last)
smallest=first;
return i;
}
return 0;
}
(数组中有 256 个元素:我知道它很乱,但它永远不会改变,而且程序不是最终版本)
谢谢
使用算法标头中的 std::min_element。
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<double> vals = { 1.0, 2.0, 0.5, 4.2 };
auto iter = std::min_element(vals.begin(), vals.end());
auto idx = iter - vals.begin();
std::cout << idx << "n";
return 0;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- 查找包含 N 个元素的数组的最小值和最大值
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- C++:仅在具有指定索引的元素中查找数组中的最小值
- 查找容器中高于阈值的最大值和最小值元素
- 查找存储最小值的元素
- 函数,它接受三个参数:指向双数组的指针、指向最小值的指针和指向最大值元素的指针
- 使用OpenCV的C++中两个图像的元素最小值
- 在容器中查找具有给定字段最小值的元素的最紧凑的方法
- 使用 CUDA 中的缩减在数组中查找最小值(但跳过某些元素)