溫馨提示×

c++輸出最小值的方法是什么

c++
小億
188
2024-06-06 16:48:20
欄目: 編程語言

在C++中,可以使用STL中的min_element函數(shù)來輸出最小值。這個(gè)函數(shù)需要在#include <algorithm>頭文件中使用。下面是一個(gè)示例代碼:

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> numbers = {4, 2, 7, 1, 9, 5};
    
    auto min_value = *std::min_element(numbers.begin(), numbers.end());
    
    std::cout << "The minimum value is: " << min_value << std::endl;

    return 0;
}

運(yùn)行上面的代碼,將會輸出數(shù)組numbers中的最小值。

0