"[닉네임]님이 들어왔습니다."
"[닉네임]님이 나갔습니다."
기존의 메시지들의 닉네임도 전부 변경 !
[유저 아이디]
로 구분채팅방 입/퇴장 기록을 어떻게 저장할까?
1) 채팅방 입/퇴장 종류, 유저 아이디, 닉네임 세가지를 다 저장하자 !
→ 세가지를 전부 저장하려면 tuple을 써야하고 복잡해지는데 다른 방법 없을까?
2) 채팅방 입/퇴장 종류, 유저 아이디만 queue에 저장하고 유저아이디로 닉네임을 찾으면 되지 않을까?
→ 입/퇴장 종류, 유저 아이디를 저장하는 queue
, 유저아이디와 닉네임을 저장하는 map
생성 !
기록들을 어떻게 저장하고 닉네임을 어떻게 변경할까?
→ Change일 경우에는 queue에 push할 필요가 없고, Leave일 경우에는 닉네임을 변경할 필요가 없다.
(즉, Change 외에는 queue에 저장, Leave 외에는 닉네임 변경)
Source Code
#include <string> #include <vector> #include <sstream> #include <queue> #include <map> #include <iostream> #define type 0 #define id 1 #define name 2 using namespace std; vector<string> solution(vector<string> record) { vector<string> answer; map <string, string> map; queue <pair<string, string>> list; for(int i=0; i<record.size(); i++) { int idx = 0; string input[3], tmp; stringstream ss(record[i]); while(ss >> tmp) { input[idx++] = tmp; } if(map.find(input[id]) == map.end()) { // 존재하지 않는 유저 map.insert(make_pair(input[id], input[name])); } if(input[type] != "Leave") { // 나가는 경우 외에 map[input[id]] = input[name]; // 닉네임 변경 } if(input[type] != "Change") { list.push(make_pair(input[type], input[id])); } } while(!list.empty()) { pair<string, string> now = list.front(); list.pop(); string result = map[now.second] + "님이 "; if(now.first == "Enter") { result += "들어왔습니다."; } else result += "나갔습니다."; answer.push_back(result); } return answer; }
#include <sstream> stringstream ss(str); // str : 분리할 문자열 int idx = 0; while(ss >> tmp) { // tmp : str에서 공백 기준으로 자른 문자열 input[idx++] = tmp; }
#include <sstream> string a, b, c; stringstream ss(str); // str : 분리할 문자열 ss >> a >> b >> c;