백준 문풀

백준 7785

조강학 2024. 5. 6. 23:32

회사에 있는 사람

공백을 구분된 두 입력을 받을 때 cin>>n>>m; 이어서 쓰면된다.

it은 interator로 it->first는 요소의 첫번째인 key를 의미하고  it->second은 요소의 value를 의미한다.

맵에 저장된 순서의 역순으로 출력하기 위해 rbegin()과 rend()를 사용하여 역순 순서로 출력할 수 있도록 해주었다.

 

 

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    map<string, string>employee;
    int num = 0;
    string name, work;
    cin >> num;
    for (int i = 0;i < num;i++) {
        cin >> name>> work;
        if (employee.find(name) != employee.end()) {
            employee.erase(name);
        }
        employee.insert({ name,work });
    }
    for (auto it =employee.rbegin();it!=employee.rend();++it) {
        if (it->second == "enter") {
            cout << it->first << "\n";
        }
    }
    
    return 0;
}

'백준 문풀' 카테고리의 다른 글

백준 11478  (0) 2024.05.06
백준 11279  (0) 2024.05.06
백준 5555  (1) 2024.03.15
백준 2908  (2) 2024.03.15
백준 1152  (0) 2024.03.10