백준 / 실버 2 / 4358 생태학 / C++

jjin·2023년 10월 5일
0

https://www.acmicpc.net/problem/4358

문자열 입력 받아 개수 세서 통계 내기

사전 순인데 예시보고 내림차순으로 출력하고있었음.. 문제 잘 보자

#include <bits/stdc++.h>
using namespace std;

bool sorter(const pair<string, int> &a, const pair<string, int> &b) {
    return a.second > b.second;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    
    string s;
    int tot;
    map<string, int> m;
    
    
    while (getline(cin, s)) {
        m[s]++;
        tot++;
    }
    vector<pair<string, int>> v(m.begin(), m.end());
    
    cout << fixed << setprecision(4);
    
    for (const auto &p : v){
        cout << p.first << " " << static_cast<double>(p.second) * 100 / tot << "\n";
    }
}

static_cast<double>

static_cast : 우리가 흔히 생각하는, 언어적 차원에서 지원하는 일반적인 타입 변환

const_cast : 객체의 상수성(const) 를 없애는 타입 변환. 쉽게 말해 const int 가 int 로 바뀐다.

dynamic_cast : 파생 클래스 사이에서의 다운 캐스팅 (→ 정확한 의미는 나중에 다시 배울 것입니다)

reinterpret_cast : 위험을 감수하고 하는 캐스팅으로 서로 관련이 없는 포인터들 사이의 캐스팅 등
https://modoocode.com/204

getline(cin, str)

str에 공백 전까지 한 줄 담음

cout << fixed << setprecision(4);

소수점 이하 4자리 고정

cout.precision(4); cout << setprecision(4);

유효숫자를 4자리로

vector<pair<T, T>> v(m.begin(), m.end());

map 순서대로 출력하고자할 때 벡터화.

for (const auto &e : C)

컨테이너 순회 시 const &를 적용하여 복사 방지

profile
진짜

0개의 댓글