c++ min of array code example

Example: min array c++

//Syntax
*min_element(ForwardIt first, ForwardIt last)
*min_element(ForwardIt first, ForwardIt last, Compare comp)
//Example:
#include <bits/stdc++.h>
using namespace std;

main() {
    vector<int> v{ 3, 1, -14, 6, 5, 9 }; 
    int result;
    
    result = *min_element(v.begin(), v.end());
    cout << "min element is: " << result << '\n'; //-14
 
    result = *min_element(v.begin(), v.end(), [](int a, int b) { return abs(a)<abs(b); });
    cout << "min element (absolute) is: " << result << '\n'; //1
}

Tags:

Cpp Example