分割用户在参数中输入的字符串

Split a string entered by the user in arguments

本文关键字:输入 字符串 参数 用户 分割      更新时间:2023-10-16

如何分割用户在参数中输入的字符串?

假设用户类型为test1 test2 test3

如何将每个输入的值分开,以使我可以分别访问这些值?

使用getline和stringstream从用户输入中提取字符,并将它们存储到字符串中,直到您通知的定界。依次使用vector存储提取的字符。

像这样:

void split(std::vector<std::string> &args, std::string userInput, char delimiter) 
{
    std::stringstream ss(userInput);
    std::string buf;
    while(getline(ss, buf, delimiter)) {
        args.push_back(buf);
    }
}