C++ Programming Ch.10 Exercise 13 Solution
Problem: map 컨ν μ΄λλ₯Ό μ΄μ©νμ¬ (μ΄λ¦, μ±μ )μ μ μ₯νκ³ μ΄λ¦μΌλ‘ μ±μ μ μ‘°ννλ μ μ κ΄λ¦¬ νλ‘κ·Έλ¨μ λ§λ€μ΄λΌ. μ΄λ¦μ λΉμΉΈ μμ΄ μ λ ₯νλ κ²μ μμΉμΌλ‘ νλ€. Execution Result: Objective & Hints: map 컨ν μ΄λ νμ© μ΄λ¦κ³Ό μ μλ₯Ό μμΌλ‘ μ μ₯ν λ§΅ 컨ν μ΄λλ‘ map<string, int>λ₯Ό μ΄μ©νλ©΄ λλ€. Code: 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 #include #include #include using namespace std; int main() { map<string, int> Score; // map 컨ν μ΄λ μμ±. ν€λ νκΈ μ΄λ¦, κ°μ μ μ μ μ cout << "***** μ μκ΄λ¦¬ νλ‘κ·Έλ¨ HIGH SCOREμ μμν©λλ€ *****\n"; while(true){ int num; int score; string name; cout << "μ λ ₯:1, μ‘°ν:2, μ’ λ£:3 >> "; cin >> num; switch (num){ case 1: cout << "μ΄λ¦κ³Ό μ μ>> "; cin >> name >> score; Score.insert(make_pair(name, score)); // mapμ μ μ₯ break; case 2: cout << "μ΄λ¦ >> "; cin >> name; if(Score.find(name) == Score.end()) // name 'ν€'λ₯Ό λκΉμ§ μ°Ύμλλ° μμ cout << "μμ" << endl; else cout << name << "μ μ μλ " <<Score[name] << endl; // Scoreμμ nameμ κ°μ μ°Ύμ μΆλ ₯ break; case 3: cout << "νλ‘κ·Έλ¨μ μ’ λ£ν©λλ€...\n"; return 0; default : cout << "μ λλ‘ μ λ ₯ν΄\n"; break; } } } Explanation: ...