티스토리 뷰

PS/PS_Note

[C/C++] STL map 활용

Aaron 2020. 5. 13. 15:43
반응형
1
#include <map>
cs


.map 자료구조

--

- 주로 string에 사용(문자열 counting, 알파벳 counting)

- Key, Value 를 pair 형태로 저장

- 균형잡힌 이진트리를 만들어가면서 map 을 구성 

- map 원소에 접근하기 위해서는 iterator 선언 필요

- Key 값 기준 오른차순으로 출력


ex) 알파벳 Counting

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
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
 
int main(void)
{
    // cin, cout 속도 향상을 위해 동기화 해제
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
 
    // file input
    ifstream cin;
    cin.open("input.txt");
 
    map<charint> ch;
    map<charint>::iterator it;
    char a[100]; // string a;
    cin >> a;
 
    for (int i = 0; a[i] != '\0'; i++)
        ch[a[i]]++;
    
    for (it = ch.begin(); it != ch.end(); it++)
        cout << it->first << ' ' << it->second << '\n';
 
    return 0;
}
cs


  - Input --------------------------------------------------------

abcdeaabbcccc

------------------------------------------------------------------


  - Output --------------------------------------------------------

a 3

b 3

c 5

d 1

e 1

------------------------------------------------------------------


ex) 단어 Counting

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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;
 
int main(void)
{
    // cin, cout 속도 향상을 위해 동기화 해제
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
 
    // file input
    ifstream cin;
    cin.open("input.txt");
 
    map<stringint> ch;
    map<stringint>::iterator it;
    char a[100]; // string a;
    int n;
 
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        ch[a]++;
    }
    
    for (it = ch.begin(); it != ch.end(); it++)
        cout << it->first << ' ' << it->second << '\n';
 
    return 0;
}
cs


  - Input --------------------------------------------------------

7

book

dog

cat

dog

cat

book

cat

------------------------------------------------------------------


  - Output --------------------------------------------------------

book 2

cat 3

dog 2

------------------------------------------------------------------


ex) 최다 빈출 단어 출력

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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;
 
int main(void)
{
    // cin, cout 속도 향상을 위해 동기화 해제
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
 
    // file input
    ifstream cin;
    cin.open("input.txt");
 
    map<stringint> ch;
    map<stringint>::iterator it;
    char a[100]; // string a;
    int n;
 
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        ch[a]++;
    }
    
    int max = 0;
    string res;
    for (it = ch.begin(); it != ch.end(); it++)
    {
        if (it->second > max)
        {
            max = it->second;
            res = it->first;
        }
    }
 
    cout << res << " : " << max << '\n';
 
    return 0;
}
cs


  - Input --------------------------------------------------------

7

book

dog

cat

dog

cat

book

cat

------------------------------------------------------------------


  - Output --------------------------------------------------------

cat 3

------------------------------------------------------------------

--


반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday