C++ 获取一个包含字符和整数的字符串,并分别使用它们C++;

C++ 获取一个包含字符和整数的字符串,并分别使用它们C++;,c++,string,get,getline,C++,String,Get,Getline,我需要将输入作为如下字符串: add 4 cancel 200 quit #include <iostream> #include <vector> #include <string> #include <sstream> // Prototype void runCommand(std::string action, int amount); void parseCommand(std::string rawCommand, std::vec

我需要将输入作为如下字符串:

add 4
cancel 200
quit
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

// Prototype
void runCommand(std::string action, int amount);
void parseCommand(std::string rawCommand, std::vector<std::string>& command);

int main()
{
    std::vector<std::string> command;       // Vector of strings that will take the command
    std::string stringInput;

    parseCommand(stringInput, command);    // Separate the raw command into different strings (command[0] and command[1])
    runCommand(command[0], stoi(command[1])); // Run the command

    return 0;
}

void runCommand(std::string action, int amount)
{
    if (action == "add")  // If the command is to add, call the add function
    {
        // add(amount);
    }
    else if (action == "cancel")   // If the command is to cancel, call the cancel function
    {
        // cancel(amount);
    }
}

// This function will take the string rawCommand, parse it into different strings,
// and put the parsed string into the vector command. Command is taken by reference
// so it can modify the original vector object.
void parseCommand(std::string rawCommand, std::vector<std::string>& command)
{   
    std::stringstream stringStream;         // Convert the string input to a std::stringstream
    std::string stringTransfer;             // This string will be a part of the command input

    stringStream << rawCommand;            // Converting the stringInput into a stringStream

    // parse the line separated by spaces
    while (std::getline(stringStream, stringTransfer, ' '))
    {
        // put the command in the vector
        command.push_back(stringTransfer);
    }
    // Clear the command to get it ready for the next potentintial command
    command.clear();
}
并将其用作命令

例如: 获取
add[n]
将告诉程序使用
add
函数并使用
int4
的值


如何一次输入这样的字符串,并稀疏地使用命令字和int?

具体地说,这取决于输入来自何处,但通常我会通过解析来接近它。大概是这样的:

add 4
cancel 200
quit
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

// Prototype
void runCommand(std::string action, int amount);
void parseCommand(std::string rawCommand, std::vector<std::string>& command);

int main()
{
    std::vector<std::string> command;       // Vector of strings that will take the command
    std::string stringInput;

    parseCommand(stringInput, command);    // Separate the raw command into different strings (command[0] and command[1])
    runCommand(command[0], stoi(command[1])); // Run the command

    return 0;
}

void runCommand(std::string action, int amount)
{
    if (action == "add")  // If the command is to add, call the add function
    {
        // add(amount);
    }
    else if (action == "cancel")   // If the command is to cancel, call the cancel function
    {
        // cancel(amount);
    }
}

// This function will take the string rawCommand, parse it into different strings,
// and put the parsed string into the vector command. Command is taken by reference
// so it can modify the original vector object.
void parseCommand(std::string rawCommand, std::vector<std::string>& command)
{   
    std::stringstream stringStream;         // Convert the string input to a std::stringstream
    std::string stringTransfer;             // This string will be a part of the command input

    stringStream << rawCommand;            // Converting the stringInput into a stringStream

    // parse the line separated by spaces
    while (std::getline(stringStream, stringTransfer, ' '))
    {
        // put the command in the vector
        command.push_back(stringTransfer);
    }
    // Clear the command to get it ready for the next potentintial command
    command.clear();
}
#包括
#包括
#包括
#包括
//原型
void runCommand(std::string action,int-amount);
void parseCommand(std::string-rawCommand,std::vector&command);
int main()
{
std::vector命令;//将接受该命令的字符串的向量
std::字符串输入;
parseCommand(stringInput,command);//将原始命令分成不同的字符串(命令[0]和命令[1])
runCommand(命令[0],stoi(命令[1]);//运行命令
返回0;
}
void runCommand(标准::字符串操作,整数金额)
{
if(action==“add”)//如果命令要添加,则调用add函数
{
//增加(金额);
}
else if(action==“cancel”)//如果命令要取消,则调用cancel函数
{
//取消(金额);
}
}
//此函数将获取string命令,将其解析为不同的字符串,
//并将解析后的字符串放入vector命令中。命令是通过引用获取的
//因此,它可以修改原始向量对象。
void parseCommand(std::string命令、std::vector和命令)
{   
std::stringstream;//将字符串输入转换为std::stringstream
std::string stringTransfer;//此字符串将是命令输入的一部分

stringStream我会使用
std::getline
获取行,然后使用
std::istringstream
分割行。我认为如果不显示您的试用版,您将无法获得帮助。您可以在
字符串中获取整行后使用
stoi
获取非数字
字符串后的
int
,然后使用n拆分该行并检查它们是否包含命令。字符串或输入行完全由字符组成。其中一些字符可能表示数字。识别这些数字字符后,可以将其转换为数值。