[STL] map์์ Value๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ
์ฐธ๊ณ ๋งํฌ: C++ STL์ ๊ฐ๋ณ๋ก ๋งต ์ ๋ ฌ - GeeksforGeeks
ํด๋น ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
๊ฒฐ๋ก ์ map STL ๋จ๋ ์ผ๋ก๋ Value ๊ธฐ์ค์ผ๋ก sortํ ์ ์๋ค.
๋ฐ๋ผ์, Pair <T,T>๋ฅผ ๋ด๋ vector์ ์ ์ธํ์ฌ map iteratior์ ๋ฐ๋ผ ์ฝ์ ํด์ฃผ๊ณ
vector์ Pair second๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค์ sortํด์ผํ๋ค.
Sorting a Map by value 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
์๋ฆฌ๊ฐ ํฅ๋ฏธ๋ก์์ ์ ๋ฆฌํด๋๋ ค๊ณ ํ๋ค.
์๋ฆฌ(์ํค๋ฐฑ๊ณผ ์ฐธ๊ณ ):
C++์ map์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ ธ๋ ๊ธฐ๋ฐ์ผ๋ก ์ด๋ฃจ์ด์ ธ์๋ ๋ ธ๋ ๊ธฐ๋ฐ์ ๊ท ํ์ด์ง tree๊ตฌ์กฐ(๋ ๋-๋ธ๋ ์ด์งํธ๋ฆฌ)๋ก ๋ง๋ค์ด์ง ๋๋ถํฐ set๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฝ์ ๋ ๋๋ถํฐ Key์ ๋ฐ๋ผ ์๋ ์ ๋ ฌ๋๋ค.

๋ ๋-๋ธ๋ ํธ๋ฆฌ๋ ๊ฐ๊ฐ์ ๋ ธ๋๊ฐ ๋ ๋ ๋ ๋ธ๋ ์ธ ์์ ์์ฑ์ ๊ฐ์ง๊ณ ์๋ ์ด์ง ํ์ ํธ๋ฆฌ์ด๋ค. ์ด์ง ํ์ ํธ๋ฆฌ๊ฐ ๊ฐ์ง๊ณ ์๋ ์ผ๋ฐ์ ์ธ ์กฐ๊ฑด์ ๋ค์๊ณผ ๊ฐ์ ์ถ๊ฐ์ ์ธ ์กฐ๊ฑด์ ๋ง์กฑํด์ผ ์ ํจํ(valid) ๋ ๋-๋ธ๋ ํธ๋ฆฌ๊ฐ ๋๋ค:
๋ ๋-๋ธ๋ ํธ๋ฆฌ - ์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์
๋ ๋-๋ธ๋ ํธ๋ฆฌ(red-black tree)๋ ์๊ฐ ๊ท ํ ์ด์ง ํ์ ํธ๋ฆฌ(self-balancing binary search tree)๋ก์, ๋ํ์ ์ผ๋ก๋ ์ฐ๊ด ๋ฐฐ์ด ๋ฑ์ ๊ตฌํํ๋ ๋ฐ ์ฐ์ด๋ ์๋ฃ๊ตฌ์กฐ๋ค. 1978๋ ๋ ์ค ๊ท๋ฐ์ค(Leo J. Guibas)์ ๋ก๋ฒํธ
ko.wikipedia.org
- ๋ ธ๋๋ ๋ ๋ ํน์ ๋ธ๋ ์ค์ ํ๋์ด๋ค.
- ๋ฃจํธ ๋ ธ๋๋ ๋ธ๋์ด๋ค.
- ๋ชจ๋ ๋ฆฌํ ๋ ธ๋๋ค(NIL)์ ๋ธ๋์ด๋ค.
- ๋ ๋ ๋ ธ๋์ ์์๋ ธ๋ ์์ชฝ์ ์ธ์ ๋ ๋ชจ๋ ๋ธ๋์ด๋ค. (์ฆ, ๋ ๋ ๋ ธ๋๋ ์ฐ๋ฌ์ ๋ํ๋ ์ ์์ผ๋ฉฐ, ๋ธ๋ ๋ ธ๋๋ง์ด ๋ ๋ ๋ ธ๋์ ๋ถ๋ชจ ๋ ธ๋๊ฐ ๋ ์ ์๋ค)
- ์ด๋ค ๋ ธ๋๋ก๋ถํฐ ์์๋์ด ๊ทธ์ ์ํ ํ์ ๋ฆฌํ ๋ ธ๋์ ๋๋ฌํ๋ ๋ชจ๋ ๊ฒฝ๋ก์๋ ๋ฆฌํ ๋ ธ๋๋ฅผ ์ ์ธํ๋ฉด ๋ชจ๋ ๊ฐ์ ๊ฐ์์ ๋ธ๋ ๋ ธ๋๊ฐ ์๋ค.
์ ์กฐ๊ฑด๋ค์ ๋ง์กฑํ๊ฒ ๋๋ฉด, ๋ ๋-๋ธ๋ ํธ๋ฆฌ๋ ๊ฐ์ฅ ์ค์ํ ํน์ฑ์ ๋ํ๋ด๊ฒ ๋๋ค: ๋ฃจํธ ๋ ธ๋๋ถํฐ ๊ฐ์ฅ ๋จผ ์๋ ธ๋ ๊ฒฝ๋ก๊น์ง์ ๊ฑฐ๋ฆฌ๊ฐ, ๊ฐ์ฅ ๊ฐ๊น์ด ์๋ ธ๋ ๊ฒฝ๋ก๊น์ง์ ๊ฑฐ๋ฆฌ์ ๋ ๋ฐฐ ๋ณด๋ค ํญ์ ์๋ค. ๋ค์ ๋งํด์ ๋ ๋-๋ธ๋ ํธ๋ฆฌ๋ ๊ฐ๋ต์ (roughly)์ผ๋ก ๊ท ํ์ด ์กํ ์๋ค(balanced). ๋ฐ๋ผ์, ์ฝ์ , ์ญ์ , ๊ฒ์์ ์ต์ ์ ๊ฒฝ์ฐ(worst-case)์์์ ์๊ฐ๋ณต์ก๋๊ฐ ํธ๋ฆฌ์ ๋์ด(๋๋ ๊น์ด)์ ๋ฐ๋ผ ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์ ๋ณดํต์ ์ด์ง ํ์ ํธ๋ฆฌ์ ๋นํด ํจ์จ์ ์ด๋ผ๊ณ ํ ์ ์๋ค.
ํด๋น ๋ธ๋ก๊ทธ๊ฐ ์์ธํ๊ฒ ์ค๋ช
์ ํด๋์๋ค. -> ์๊ณ ๋ฆฌ์ฆ ) Red-Black Tree (tistory.com)
๋ฐฉ๋ฒ 1 – ์์ ๋ฒกํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์์ด๋์ด๋ ๋งต์์ ํด๋น ์ ๋ฒกํฐ์ ๋ํ ๋ชจ๋ ๋ด์ฉ์ ๋ณต์ฌํ๊ณ ์๋์ ์ฃผ์ด์ง lambda ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋ฒ์งธ ๊ฐ์ ๋ฐ๋ผ ์์ ๋ฒกํฐ๋ฅผ ์ ๋ ฌ
bool cmp(pair<T1, T2>& a,
pair<T1, T2>& b)
{
return a.second < b.second;
}
where T1 and T2
are the data-types
that can be the
same or different.
๋ค์์ ์์ ์ ๊ทผ ๋ฐฉ์์ ๊ตฌํ
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Comparator function to sort pairs
// according to second value
bool cmp(pair<string, int>& a,
pair<string, int>& b)
{
return a.second < b.second;
}
// Function to sort the map according
// to value in a (key-value) pairs
void sort(map<string, int>& M)
{
// Declare vector of pairs
vector<pair<string, int> > A;
// Copy key-value pair from Map
// to vector of pairs
for (auto& it : M) {
A.push_back(it);
}
// Sort using comparator function
sort(A.begin(), A.end(), cmp);
// Print the sorted value
for (auto& it : A) {
cout << it.first << ' '
<< it.second << endl;
}
}
// Driver Code
int main()
{
// Declare Map
map<string, int> M;
// Given Map
M = { { "GfG", 3 },
{ "To", 2 },
{ "Welcome", 1 } };
// Function Call
sort(M);
return 0;
}
Welcome 1
To 2
GfG 3
[C++] std::map์ value ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ - doodoo's coding
I really love solving computer problem. I wanna grow today more than yesterday.
0xd00d00.github.io