[알고리즘 + 자료구조]/[프로그래머스]

프로그래머스 오픈채팅방

Hevton 2022. 9. 23. 23:45
반응형

 

새로 알게된 자료구조 MAP (https://kbj96.tistory.com/23)

#include <map>

map<자료형, 자료형> m

key와 value의 쌍으로 관리되는 map이다.

중복을 허용하지 않는게 특징이다.

 

 

이 MAP과 stringstream을 이용해서 문제를 풀었다.

 

프로그래머스 오픈채팅방

#include <string>
#include <vector>
#include <sstream>
#include <map>

using namespace std;

vector<string> solution(vector<string> record) {
    vector<string> answer;
    
    map<string, string> m; // uid, nickname

    string command, uid, nickname;

    for(auto x : record) {
        
        stringstream ss(x);
                
        ss >> command >> uid >> nickname;
        
        // 들어왔거나 change되었을 때에만 등록하면 됨
        if(command == "Enter" || command == "Change")
            m[uid] = nickname;
        
    }
    
    vector<string> v;
    
    for(auto x : record) {
        
        stringstream ss(x);
                
        ss >> command >> uid;
        
        if(command == "Enter") {
           
            v.push_back(m[uid]+"님이 들어왔습니다.");
            
        } else if(command == "Leave") {
            
            v.push_back(m[uid]+"님이 나갔습니다.");
        }
        
        
    }
    

    return v;
}

 

 

문자열 내 구분에서는 stringstream이 최고다..

 

반응형