Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- data
- 16197
- intent
- git
- 안드로이드
- 데이터
- insert
- 백준
- service
- broadcastreceiver
- 큐빙
- IntelliJ
- Java
- Jenknis
- 제어반전
- activity
- 두 동전
- Algorithm
- ubuntu
- 알고리즘
- 프로그래머스
- Android
- 단축키
- 17837
- vscode
- github
- mysql
- 데이터전달
- spring
- goland
Archives
- Today
- Total
해보자
[map] value sort ( C++ ) 본문
1. map → set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <iostream>
#include <set>
#include <map>
using namespace std;
struct setCompare {
bool operator()(const pair<int, int> &lhs, const pair<int, int> &rhs) {
if (lhs.second == rhs.second) return lhs.first < rhs.first;
return lhs.second < rhs.second;
}
};
int main() {
map<int, int> m;
set<pair<int, int>, setCompare> s;
for (auto it = m.begin(); it != m.end(); it++) {
s.insert(*it);
return 0;
}
|
cs |
2. map → vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
bool cmp(pair<int, int> left , pair<int, int> right ) {
return left.second < right.second;
}
int main() {
map<int, int> m;
vector <pair<int, int>> v;
// key-value 값 복사
copy(m.begin(), m.end(), back_inserter<vector<pair<int, int>>>(v));
// value 기준 정렬
sort(v.begin(), v.end(), cmp);
return 0;
}
|
cs |
'C++ > Container' 카테고리의 다른 글
[vector] find, distance (C++) (0) | 2020.02.05 |
---|