当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Split a string entered by the user in arguments

问题描述:

how can I split a string entered by the User in arguments?

assuming that a user type: test1 test2 test3

how can i separate each entered values, in a way that i can access these values separately?

网友答案:

Use getline with stringstream to extract characters from user input and stores them into string until the delimitation you inform. sequentially use a vector to store the extracted characters.

something like this:

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);
    }

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: