关于我用AI编写了一个聊天机器人……(番外1)
极大地精简了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; }