C++ 查找浮点数据容器中的最大值
以下是一个示例的 C++ 代码,用于创建一个浮点数据容器并输出其内数据的最大值:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<float> container; // 创建浮点数据容器
// 添加数据到容器
container.push_back(1.5);
container.push_back(2.3);
container.push_back(0.8);
container.push_back(4.1);
container.push_back(3.7);
// 查找容器中的最大值
float max_value = *std::max_element(container.begin(), container.end());
// 输出最大值
std::cout << '最大值为: ' << max_value << std::endl;
return 0;
}
在上述代码中,我们使用了 std::vector 容器来存储浮点数据。然后,我们使用 push_back() 函数向容器中添加数据。接下来,我们使用 std::max_element() 函数找到容器中的最大值,并将其赋值给 max_value 变量。最后,我们使用 std::cout 来输出最大值。
原文地址: https://www.cveoy.top/t/topic/qBVe 著作权归作者所有。请勿转载和采集!