http://www.cplusplus.com/reference/algorithm/sort/
기본 사용법
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8);
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// compare all
sort (myvector.begin(), myvector.end());
}
내림차순
#include <string>
#include <vector>
#include <functional> // greater
#include <algorithm>
using namespace std;
int main(){
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8);
sort (myvector.begin(), myvector.end(), greater<int>());
}
나만의 비교함수 만들기
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main(){
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8);
// using function as comp, ascending order
sort(myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
}
'알고리즘 문제풀이 > 알고리즘' 카테고리의 다른 글
[c++] substr 함수 (0) | 2020.04.15 |
---|---|
[c++] priority_queue 함수 / 우선순위 큐란? (0) | 2020.04.11 |
[c++] 문자열, 숫자 변환 / stoi 함수 / to_string 함수 / (0) | 2020.03.27 |
[c++] 완전탐색(Brute-Force) / 순열 / next_permutation 함수 (0) | 2020.03.27 |
[c++] DFS(깊이 우선 탐색) / 개념 / 함수 구현 (1) | 2020.03.03 |