일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 그래프
- 이진탐색
- Brute Force
- Hash
- String
- union find
- 스토어드 프로시저
- binary search
- Two Points
- Stored Procedure
- Dijkstra
- MYSQL
- DP
- SQL
- two pointer
- 다익스트라
- Trie
Archives
- Today
- Total
codingfarm
vector 정렬하기 본문
임의의 클래스에 대해 sort함수로 정렬할 기준을 세우는것은 operator를 overloading 하면 되지만,
원하는 순간마다 사용자가 원하는 정렬을 하기는 쉽지 않다.
그럴때는 아래의 방법을 사용하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include<iostream>
#include<vector>
#include<random>
#include<algorithm>
using namespace std;
class Node {
public:
int a, b;
Node(int a, int b): a(a), b(b){}
Node():Node(-1,-1){}
friend ostream& operator<<(ostream& os, const Node &node) {
os << "a : " << node.a << ", b : " << node.b << endl;;
return os;
}
};
vector<Node> nodes_a, nodes_b;
bool CompA(const Node& lhs, const Node& rhs) {
return lhs.a < rhs.a;
}
bool CompB(const Node& lhs, const Node& rhs) {
return lhs.b < rhs.b;
}
int main(void) {
for (int i = 0; i < 10; i++) {
nodes_a.push_back(Node(rand() % 100, rand() % 100));
nodes_b.push_back(Node(rand() % 100, rand() % 100));
}
sort(nodes_a.begin(), nodes_a.end(), CompA);
sort(nodes_b.begin(), nodes_b.end(), CompB);
cout << "case A" << endl;
for (int i = 0; i < nodes_a.size(); i++)
cout << nodes_a[i];
cout << "\n\n";
cout << "case A" << endl;
for (int i = 0; i < nodes_b.size(); i++)
cout << nodes_b[i];
return 0;
}
|
cs |
'Programming Language > C++' 카테고리의 다른 글
프로젝트 셋팅 - 미리 컴파일된 헤더(Precompiled Header) (0) | 2021.05.05 |
---|---|
map 활용 (0) | 2021.04.22 |
std::initializer_list (0) | 2021.04.17 |
std::vector의 push_back과 emplace_back의 차이 (0) | 2021.04.16 |
std::move (0) | 2021.04.16 |
Comments