본문 바로가기
c++

[C++] STL - <algorithm>

by yoonjunho 2023. 2. 25.

STL

4개로 구성되어 있다. (이것들을 미리 만들어 놓은 라이브러리이다.)

-algorithms <- 지금 여기

-containers

-functions

-literators

 

 

 

1. std::stable_sort

https://zoosso.tistory.com/1085

 

stable_sort()와 sort() 차이 알아보기

배열이나 vector 원소를 정렬하는데 있어서 자주 사용되는 sort() C++에는 stable_sort()라는 것도 있다. 📌 [C++] sort() 함수란? [C++] sort() 함수란? 개발할 때나 코딩 테스트에서 많이 활용되는 정렬(Sort)

zoosso.tistory.com

사용방법 : 

#include <algorithm>
bool cmp(member a, member b) {
	return a.age < b.age;
}
vector <member> sv;
stable_sort(sv.begin(), sv.end(), cmp);

sv는 벡터이다. 

sv.begin()은 벡터 sv의 0번 인덱스(시작점)의 주솟값이다.

sv.end()는 벡터 sv의 마지막 인덱스(끝점)의 주솟값이다. 

cmp는 비교하는 함수이다. bool값을 리턴하여, return 첫 번째 인자 < 두 번째 인자  이면 오름차순,

                                                                           return 첫 번째 인자 > 두 번째 인자 이면 내림차순으로 정렬한다. 

 

stable_sort는 내부적으로 merge sort로 구현되어 있다. 

merge sort는

https://gmlwjd9405.github.io/2018/05/08/algorithm-merge-sort.html

이런 시간 복잡도를 가지고 있다. 즉, 항상 O(n x log n)의 시간 복잡도를 보장하는 정렬 알고리즘이다.

 

https://www.geeksforgeeks.org/stable_sort-c-stl/

 

stable_sort() in C++ STL - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

2. std::max함수

 

#include <algorithm>

using namespace std;

 

int a=1;

int b=2;

 

cout<<max(a,b);이면 둘 중에 큰 수인 a를 리턴한다. 

 

▶ std::max 함수 원형

헤더 : <algorithm>

'c++' 카테고리의 다른 글

[C++] 절댓값 구하기  (0) 2023.02.27
[C++] STL - containers  (0) 2023.02.25
[c++] STL  (0) 2023.02.20
[c++]-STL-컨테이너-string  (0) 2023.02.20
[c++] STL - 컨테이너 - vector  (0) 2023.02.20