c++ - 了解数组c ++中最小值位置的最简单方法
问题描述
我有这段代码我怎样才能找到读数数组中的最小值位置。例如,如果最小值是读数 [10],有没有办法我可以编写代码
int count = 0;
long readings[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (count = 0;count <21;count++;){
readings [count] = time2[count] + (time3[count] * 60) + ( time4[count] * 3600);
}
我实际上是在像这样使用 if 语句。
if ( (readings[0] <= readings[1]) && (readings[2]) ) {}
但现在我想计算并比较数组中的 20 个数字(读数 [0])与(读数 [20]),如果有一种方法可以让我知道数组中的最小元素位置,我将不胜感激。
解决方案
std::min_element
将为您完成工作:
auto index = std::distance(std::begin(readings), std::min_element(std::begin(readings), std::end(readings)));
推荐阅读
- c# - C# Excel Interop Chart Column Series Problem
- deep-learning - 如何计算人工智能或深度学习任务中的时钟频率(频率)?
- python - 使用逻辑回归时如何打印特征重要性的简单列表?
- c# - 如何创建一个 List(of T),其中 T 是 (T) 的接口,并且 List 的内容继承了一个实现 T 接口的抽象类
- cloudinary - cloudinary 下载PDF
- electron - 如何使电子预加载文件使用相同的类实例
- c++ - C++11 - 为什么编译器不优化对 const 左值引用绑定的右值引用?
- typescript - Vue + typescript:如何用类型注释计算?
- ios - 解析数组中的嵌套 json,如树 swifty json
- windows - 如何将配置文件传递到 docker for windows