Fri Dec 27 2019
STL Min Max
C++ Programming2752 views
File Name: stl-min-max.cpp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define get_size(array) (sizeof((array))/sizeof((array[0])))
int main() {
int number[] = {3,5,7,1,2,4,6,8,9,0};
char letter[] = {'o','t','w','s','g','k','e','b'};
vector<int> num( number, number + get_size(number));
vector<char> lttr( letter, letter + get_size(letter));
/* Find maximun value */
cout << "Maximum number: " << max(number[0], number[1]) << endl;
cout << "Maximum String: " << max(letter[0], letter[1]) << endl;
/* Find minimum value */
cout << "Minimum number: " << min(number[0], number[1]) << endl;
cout << "Minimum String: " << min(letter[0], letter[1]) << endl;
/* Find maximum element */
cout << "Largest number from the list: " << *max_element(num.begin(), num.end()) << endl;
cout << "Largest String from the list: " << *max_element(lttr.begin(), lttr.end()) << endl;
/* Find minimum element */
cout << "Smallest number from the list: " << *min_element(num.begin(), num.end()) << endl;
cout << "Smallest String from the list: " << *min_element(lttr.begin(), lttr.end()) << endl;
return 0;
}
/* Output */
Maximum number: 5
Maximum String: t
Minimum number: 3
Minimum String: o
Largest number from the list: 9
Largest String from the list: w
Smallest number from the list: 0
Smallest String from the list: b
Reference:
Author:Geekboots