Is There A Built-In Way to Split Strings In C++?

前端 未结 10 2250
心在旅途
心在旅途 2020-12-08 10:56

well is there? by string i mean std::string

10条回答
  •  广开言路
    2020-12-08 11:34

    The answer is no. You have to break them up using one of the library functions.

    Something I use:

    std::vector parse(std::string l, char delim) 
    {
        std::replace(l.begin(), l.end(), delim, ' ');
        std::istringstream stm(l);
        std::vector tokens;
        for (;;) {
            std::string word;
            if (!(stm >> word)) break;
            tokens.push_back(word);
        }
        return tokens;
    }
    

    You can also take a look at the basic_streambuf::underflow() method and write a filter.

提交回复
热议问题