본문 바로가기

알고리즘/기본 문법

<알고리즘> 배열에서 가장 큰 값(max_element()), 가장 작은 값(min_element())

인자로는 first iterator, last iterator를 넣어주면 된다.

 

 

 

 

코드


#include <bits/stdc++.h>
using namespace std;

int main()
{
	vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int max = *max_element(v.begin(), v.end()); // iterator로 리턴되서 *를 붙여서 값을 받음 
	int min = *min_element(v.begin(), v.end());
	
	cout << "max : " << max << "\n";
	cout << "min : " << min << "\n";
	
	return 0;
}

/*
	max : 10
	min : 1
*/