관리 메뉴

기억을 위한 기록들

[CPP] std::min_element 최소값/ std::max_element 최대값 구하기 본문

C & CPP

[CPP] std::min_element 최소값/ std::max_element 최대값 구하기

에드윈H 2023. 11. 26. 17:21

 

 

vector, list 등을 사용하다보면 각 데이터들에서 최소값 최대값을 구할때가 필요한데 유용한 std 함수가 있다.

template <class ForwardIterator>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last);
  
template <class ForwardIterator, class Compare>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp);

 

 

template <class ForwardIterator>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);
  
template <class ForwardIterator, class Compare>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp);

 

 

해당 함수를 사용하면 각각 mix max에 따라 최대값 최소값을 쉽게 구할 수 있고, 혹여나 입맛에 따른 comp 함수를 사용할 수 있다.

 

해당 함수의 장점은 반환값으로 iterator를 뱉어주고 있다는 점이다.

 

 

활용 예 )

 

https://hyo-ue4study.tistory.com/577

 

[Code Wars] Remove the minimum(C++)

https://www.codewars.com/kata/563cf89eb4747c5fb100001b/cpp Codewars - Achieve mastery through coding practice and developer mentorship A coding practice website for all programming levels – Join a community of over 3 million developers and improve your c

hyo-ue4study.tistory.com