極大地精簡了1.3.6版本的邏輯。
不會作為正式版發布。
未填充數據。
核心結構
代碼包含兩個主要部分:
數據結構:
- 使用
map<string, string>
存儲問答對,其中鍵是問題,值是答案
- 使用
主程序流程:
- 初始化預定義的問答對
- 進入無限循環接收用戶輸入
- 在 map 中查找匹配的問題并輸出答案
- 未找到匹配時輸出默認回答
#include <iostream> #include <string> #include <map> using namespace std; int main() {map<string, string> qa;qa["123"] = "456";qa["456"] = "789";cout << "Hello!" << endl;string input;while (true) {cin >> input;map<string, string>::iterator it=qa.find(input);if (it != qa.end()) {cout << "Robot: " << it->second << endl;} else {cout << "Robot: I don't know how to answer.\n";}}return 0; }