-
C++/연산자 오버로딩/operatior overloadingComputerScience/STL 2020. 5. 24. 17:58
연산자 오버로딩이 어려워 정리해봤다
#include <stdio.h> #include <algorithm> #include <queue> #include <vector> using namespace std; bool cmp_func1(const int& a, const int& b) { return a < b; } bool cmp_func2(const int& a, const int& b) { return a > b; } struct cmp_struct1{ bool operator()(const int& x, const int& y) { return x < y; } }; struct cmp_struct2{ bool operator()(const int& x, const int& y) { return x > y; } }; int main() { priority_queue <int>q1; // 4 3 2 1 0 priority_queue <int ,vector<int>, greater<int> > q2; // 0 1 2 3 4 priority_queue <int, vector<int>, less<int> > q3; // 4 3 2 1 0 priority_queue <int, vector<int>, cmp_struct1 > q4; // 4 3 2 1 0 priority_queue <int, vector<int>, cmp_struct2 > q5; // 0 1 2 3 4 vector <int>v1; vector <int>v2; vector <int>v3; vector <int>v4; vector <int>v5; for (int i = 0; i < 5; i++) { q1.push(i); v1.push_back(i); q2.push(i); v2.push_back(i); q3.push(i); v3.push_back(i); q4.push(i); v4.push_back(i); q5.push(i); v5.push_back(i); } sort(v2.begin(), v2.end(),greater<int>()); sort(v3.begin(), v3.end(), less<int>()); sort(v4.begin(), v4.end(), cmp_func1); sort(v5.begin(), v5.end(), cmp_func2); printf("priority_queue \n\n"); for (int i = 0; i < 5; i++) { printf("%d ", q1.top()); q1.pop(); } printf(": 그대로 넣었을 때"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", q2.top()); q2.pop(); } printf(": greater<int>"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", q3.top()); q3.pop(); } printf(": less<int>"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", q4.top()); q4.pop(); } printf(": return x < y struct 넣었을 때"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", q5.top()); q5.pop(); } printf(": return x > y struct 넣었을 때"); puts("\n"); puts("\n"); //----------------------------------------// printf("vector \n\n"); for (int i = 0; i < 5; i++) { printf("%d ", v1[i]); } printf(": 그대로 넣었을 때"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", v2[i]); } printf(": greater<int>()"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", v3[i]); } printf(": less<int>()"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", v4[i]); } printf(": return x < y func 넣었을 때"); puts("\n"); for (int i = 0; i < 5; i++) { printf("%d ", v5[i]); } printf(": return x > y func 넣었을 때"); puts(""); }
결과값
편의를 위해 int로 진행하였다.
priority_queue는 원래는 max_heap이다.
vector는 원래는 무작위 입력이지만 오름차순으로 기본 입력을 진행했다.
priority_queue는 struct를 이용해 연산자 오버로딩을 해야 하고
vector는 bool 함수를 통해 오버로딩한다.
방법은
내가 만든 구조체를 쓰려면
priority_queue <int, vector<int>, cmp_struct1 > q;
혹은 less와 greater을 쓰기 위해선
priority_queue <int, vector<int>, greater<int> >q;
요렇다.
vector의 sort는 조금 차이가 있는데
sort(v.begin(),v.end(), cmp);
혹은 less와 greater을 쓰려면
sort(v.begin(),v.end(), greater<int>());
'ComputerScience > STL' 카테고리의 다른 글
[C++] fill 함수 (0) 2020.07.23 [C++/STL] set은 무엇인가? [중복제거용, 존재체크용] (0) 2020.05.27 [C++/priority_queue/operator overloading] 우선순위 큐 struct 연산자 오버로딩 (2) 2020.05.24 [C++/STL]Vector (0) 2020.05.21