Blog Content

    티스토리 뷰

    범위 기반 for문 및 문자열 분할

    1. 범위 기반 for문


    1
    2
    3
    4
    5
    6
    for (string input : answer)  // 아래의 코드와 같은 구문
        cout << input << ' ';
     
    for (vector<string>::iterator input = answer.begin(); input != answer.end(); ++input)
        cout << *input << ' ';
     
    cs



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    for (vector<string> input : relation) {
        for (string in : input)
            cout << in << ' ';
        cout << endl;
    }
     
    for (vector<vector<string>>::iterator input = relation.begin(); input != relation.end(); ++input) {
        for (vector<string>::iterator in = (*input).begin(); in != (*input).end(); ++in)
            cout << *in << ' ';
        cout << endl;
    }
    cs


    2. 문자열 분할


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    for (string input : record)
    {
        stringstream ss(input);
        ss >> command;
        ss >> uid;
        if (command == "Enter" || command == "Change")
        {
            ss >> ID;
            m[uid] = ID;
        }
    }
    cs


    3. 문제를 통한 예시(카카오 코딩 테스트 1번 오픈채팅방)


    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
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iostream>
    #include <map>
    using namespace std;
     
     
    vector<string> solution(vector<string> record) {
        vector<string> answer;
        string command;
        string ID;
        string uid;
        map<stringstring> m;
     
     
        for (auto input : record)
        {
            stringstream ss(input);
            ss >> command;
            ss >> uid;
            if (command == "Enter" || command == "Change")
            {
                ss >> ID;
                m[uid] = ID;
            }
        }
     
        for (string input : record)
        {
            stringstream ss(input);
            ss >> command;
            ss >> uid;
            if (command == "Enter")
            {
                ID = (m.find(uid)->second);
     
                string temp = ID + "님이 들어왔습니다.";
                answer.push_back(temp);
     
            }
            else if (command == "Leave")
            {
                ID = (m.find(uid)->second);
                string temp = ID + "님이 나갔습니다.";
                answer.push_back(temp);
            }
        }
        return answer;
    }
     
    int main()
    {
        vector<string> answer;
        answer = { "Enter uid1234 Muzi""Enter uid4567 Prodo"
            "Leave uid1234""Enter uid1234 Prodo""Change uid4567 Ryan" };
        for (vector<string>::iterator input = answer.begin(); input != answer.end(); ++input)
        {
            cout << *input << ' ';
        }
        return 0;
    }
    cs


    Comments