我想从文本文件中读取命令

I want to read commands from a text file

我想阅读我的邻接列表图的命令。

5
ADD 0 1
ADD 1 2
ADD 2 3
ADD 3 1
ADD 2 0
LIST 2
DFS 2
BFS 2
REMOVE 1 

我不明白如何从文件中读取它并将相应的函数调用为 addEdge(0,1) 用于 ADD 0 1 和所有其他函数。

我尝试使用 getline(),但我不明白如何从字符串中获取数字。

没有任何方法可以从文件中读取,就像文件只有我们可以使用的 ADD 命令一样 while(file >> str >> num1 >> num2)

Isn't there any way to read from a file like if the file only had ADD commands we could use

while(file >> str >> num1 >> num2)

您可以使用一些代码,例如

 std::string command;
 while(file >> command) {
     if(command == "ADD") {
         int num1, num2;
         if(!(file >> num1 >> num2) {
             std::err << "Unexpected input error" << std::endl;
         }
         else {
             addEdge(num1,num2);
         }
     else if(command == "LIST")
         int num1;
         if(!(file >> num1) {
             std::err << "Unexpected input error" << std::endl;
         }
         else {
             list(num1);
         }
     }
     else if(command == "DFS") {
        // etc.
     }
     // etc.
 }